I'm looking for a way to replace all instances of a form:
model->variable
with
models[variable][index]
where variable can be pretty much any combination of letters and numbers, probably defined like [0-9a-Z]{4,12}.
There are hundreds of such variables in the text. I need to know exact form of found string "variable" to use it in replacement. Is there a way to "remember" the string and use it later? Or any other method / software which could help in such case?
Thanks in advance.
If you could convert "variable" to uppercase by the way, it would be awesome.
You can use things in the pattern to replace with if you enclose it in \(...\). You then use \1 to insert the thing that was captured by the first such bracket.
A naïve solution to your problem would be this:
sed 's/model->\(.*\)/models[\1][index]/' file.txt
Related
I would like to find out how you can tell if a character in a variable is a letter or number
for example:
if I used the code: ABC123
How would I find out if a variable followed that pattern so if a inputted code would be DNM567 it would print "correct"
But if a code was DNM56T it would print "incorrect".
Many thanks
You can use regular expressions, or linearly scan the character array to ensure that no letter comes after a number.
More information about the question would be helpful.
you can use regular expression:
if(Regex.IsMatch(myString, "^[A-Za-z]{3}[0-9]{3}$"))
{
// you got the right pattern...
}
edit: this is C# but regular expression can be found in almost any OOP language out there.
I have an NSArray of lines (objective-c iphone), and I'm trying to find the line which starts with a number, followed by a dot and a space, but can have any number of spaces (including none) before it, and have any text following it eg:
1. random text
2. text random
3.
what regular expression would I use to get this? (I'm trying to learn it, and I needed the above expression anyway, so I thought I'd use it as an example)
With C#:
#"^ *[0-9]+\. "
It doesn't check for the presence of something after the ., so this is legal:
1.(space)
If you delete the # and escape the \ it should work with other languages (it is pretty "down-to-earth" as RegExpes go)
I may suggest (Perl-compatible regexp):
^\s*\d+\.\s
At the beginning of a line:
Any number (0-n) of spaces
One or more digits
A dot
A space
Something like
^\s*\d+\.
But it depends on the language.
/^\s*[0-9]+\.\s+/
would be my guess providing you don't have any space before the number
"artistName":"Travie McCoy", "collectionName":"Billionaire (feat. Bruno Mars) - Single", "trackName":"Billionaire (feat. Bruno Mars)",
i wish to get the artist name so Travie McCoy from within that code using regex, please not i am using regexkitlite for the iphone sdk if this changes things.
Thanks
"?artistName"?\s*:\s*"([^"]*)("|$) should do the trick. It even handles some variations in the string:
White space before and after the :
artistName with and without the quotes
missing " at the end of the artist name if it is the last thing on the line
But there will be many more variations in the input you might encounter that this regex will not match.
Also you don’t want to use a regex for matching this for performance reasons. Right now you might only be interested in the artistName field. But some time later you will want information from the other fields. If you just change the field name in the regex you’ll have to match the whole string again. Much better to use a parser and transform the whole string into a dictionary where you can access the different fields easily. Parsing the whole string shouldn’t take much longer than matching the last key/value pair using a regex.
This looks like some kind of JSON, there are lots of good and complete parsers available. It isn’t hard to write one yourself though. You could write a simple recursive descent parser in a couple of hours. I think this is something every programmer should have done at least once.
\"?artistName\"?\s*:\s*\"([^\"]*)(\"|$)
Thats for objective c
I feel silly for asking this but it isn't like I could google this.
What is the ` character called? In case it doesnt show up, it is the character used for inline code with markdown. Also, on most keyboards, it shares the key with ~.
I like all three answers so I made this a CW instead of accepting
All sorts of things, but in programming mostly the back-quote or backtick,
Grave (pronounced Grahv, not like the synonym for tomb) or Grave accent.
From the Jargon file, the prime nerd reference which really should be an ISO standard :-)
Common: backquote; left quote; left single quote; open quote; ; grave.
Rare: backprime; backspark; unapostrophe; birk; blugle; back tick; back glitch; push; opening single quotation mark; quasiquote.
You can use Unicode table to find name of the symbol. There are utilities which let you search it, like gucharmap. It gives U+0060 GRAVE ACCENT for this symbol.
This answer or this answer provides a good definition.
In laymen's terms "no-shift-tilde" is also useful in PHP for keeping mySQL statements from crashing on single quotes on the table name.
SELECT * from `the_table_name` WHERE id=1 // best practice
For some reason certain PHP servers will choke on this:
SELECT * from 'the_table_name' WHERE id=1 // not preferred method
This normally works, but doesn't pass nice in strings:
SELECT * from "the_table_name" WHERE id=1 // how to escape this string?
Other than that, I don't use it much.
My goal is to find all "<?=" occurrences with ack. How can I do that?
ack "<?="
Doesn't work. Please tell me how can I fix escaping here?
Since ack uses Perl regular expressions, your problem stems from the fact that in Perl RegEx language, ? is a special character meaning "last match is optional". So what you are grepping for is = preceded by an optional <
So you need to escape the ? if that's just meant to be a regular character.
To escape, there are two approaches - either <\?= or <[?]=; some people find the second form of escaping (putting a special character into a character class) more readable than backslash-escape.
UPDATE As Josh Kelley graciously added in the comment, a third form of escaping is to use the \Q operator which escapes all the following special characters till \E is encountered, as follows: \Q<?=\E
Rather than trying to remember which characters have to be escaped, you can use -Q to quote everything that needs to be quoted.
ack -Q "<?="
This is the best solution if you will want to find by simple text.
(if you need not find by regular expression.)
ack "<\?="
? is a regex operator, so it needs escaping