This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Suppose we have a string:
some random text $$ hello world $$ some more stuff
In the string delimited by $$, I would like to capture all occurrences of o (for example, in order to find and replace in Sublime Text 3).
How do I formulate such a regex command? Even though I plan to use this regex command in Sublime Text, I can take a regex command that uses different notation and fix it for use in ST3.
Use the regex:
(\$\$[^o]*)o(.*?\$\$)
replace with:
\15\2
where 5 is the substitution string.
Test here.
If you want to search for a sub-string instead of only one character, you may use:
(\$\$.*?)lo(.*?\$\$)
with the same replacement string.
Test here.
Another option is to extract first the sub-string between the delimiters ($$ hello world $$), and then on the result perform any search-and-replace action you need. This might also need looping until no replacements are done any more.
To the best of my regex knowledge, you'll need to use two regular expressions to replace all the o's between the delimiters. The first one would be to actually grab the text within the delimiter. For example:
(?P<start>\$\$) # start by grabbing the starting literal '$$'
(?<middle>.*?)(?=\$\$) # then grab everything up until the ending '$$'
(?P<end>\$\$) # now grab the ending '$$'
Example: https://regex101.com/r/015Xj8/4
Obviously you know what the start and ending is so you can simplify it further if you wanted (no start or end group for example), but I've included that for thoroughness.
Once you've captured the start/end of it, you can replace the o by a straight find/replace on the literal 'o'. As far as I know, it takes two steps via regex to do what you're after. I'm not too knowledgeable about sublime but perhaps there's a sed-like replacement feature in it.
The following will replace the first o in the string (similar to the other answer by virolino), but you'll need to click "Enter" a bunch of times to make sure that all the last o's are replaced for this to be useful:
(?P<start>\$\$)
(?<middle>.*?(?<first_o>o).*?)(?=\$\$)
(?P<end>\$\$)
Or, if you're just looking to capture O's (and nothing else), you can just make sure it's between a starting and ending $$:
\$\$.*?(o).*?\$\$
Related
This question already has answers here:
Regex to get string between curly braces
(16 answers)
Closed 2 years ago.
How do I use regex to get what is inside of a ${} enclosed value such as:
Dont care about this. ${This is the thing I want regex to return} Not this either.
Should return:
This is the thing I want regex to return
I've tried \${ }$
the best I got messing around on regex101.com
I'll be honest I have no Idea what I'm doing as far as regex goes
using on c++ but would also (if possible) like to use in geany text editor
I suggest \${[^}]*}. Note that $ have special meaning in regular expressions and need to be escaped with a \ to be read literary.
I use [^}]* instead of .* between the braces to avoid making a long match including the entire value of:
${Another} match, more then one ${on the same line}
[^}] means anything but }
What you want is matching the starting ${ and the ending } with any amount of characters in between: \$\{.*\}. The special part here is the .*, . means any character and * means the thing in front of it can be matched 0 or more times.
Since you want thre matched results, you might also want to wrap it in (): (\$\{.*\}). The parenthesis makes regex remember the stuff inside for later use.
See this stackoverflow on how to get the results back:
How to match multiple results using std::regex
This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
I am trying to quickly find all .java files which contain one term but are missing another term. I'm using MyEclipse 10.7 and its 'Search | File Search' feature, which supports regular expressions.
Will regex work in this scenario? What would the correct regex be?
The only solution I could find to work is the following Regex:
^(?!.[\s\S]*MISSING_TERM).[\s\S]*INCLUDED_TERM.*$
It finds every file which includes INCLUDED_TERM but lacks MISSING_TERM, regardless of the line.
The key is the \s\S, which ensures the whole file is searched and not each line.
If you want to find it on a single line, use it like this:
^(?!.*MISSING_TERM).*INCLUDED_TERM.*$
You can also use \ as an escape character, cause you may need it like class\.variable.
You could use something like:
(?<!.*bar)foo(?!.*bar)
Will match if "foo" is found but "bar" is not.
Notice: you must configure your search engine to use multiline regex (EX: Notepad++ has an option called ". matches newline") because usually the dot represent any character except line break.
(?m)\A(?=.*REGEX_TO_FIND)(?!.*MISSING_REGEX.*).*\z
The regex can get kinda tricky but it breaks down into two pieces.
Find the matching term/phrase/word. This part isn't too tricky as this is what regex normally looks for.
Finding the term not present. This is the tricky part, but it's possible.
I have an example HERE which shows how you want to find the word connectReadOnly in the text, and fail to find disconnect. Since the text contains connectReadOnly it starts looking for the next piece, not finding disconnect. Since disconnect is in the text it fails on the entire string (what you will need for your entire file to match). If you play around with the second piece, the negation part (?!.*disconnect.*), you can set that as whatever regex you need. In my example I don't want to find disconnect anywhere in my code :) You can easily replace that with your word to search on, or even a more complex regex to "not find".
The key is to use multi line mode, which is set using the beginning (?m) and then using the start/end of string chars. Using ^ and $ to start/end a line, where \A and \z start and end a string, thus extending the match over the entire file.
EDIT: For the connectReadOnly and disconnect question use: (?m)\A(?=.*connectReadOnly)(?!.*disconnect.*).*\z. The updated example can be found here.
I am facing a problem whereby I am given a string that contains a path to a file and the file's name and I only want to extract the path (without the file's name)
For example, I will receive something like
C:\Users\OopsD\Projects\test.acdbd
and from that string I want to extract only
C:\Users\OopsD\Projects
I was trying to create a RegEx to match a backslash followed by a word, followed by a dot followed by another word - this is to match the
\test.acdbd
part and replace it with empty string so that the final result is
C:\Users\OopsD\Projects
Can anyone, familiar with RegEx, help me on this one? Also, I will be using regular expressions quite a lot in the future. Is there a (free) program I can download to create regular expressions?
Are you really sure you need to be using Regex for such as simple task? How about this:
Dim file As New IO.FileInfo(" C:\Users\OopsD\Projects\test.acdbd")
MsgBox(file.Directory.FullName)
Regarding the free program on Regex, I would definitely recommend http://www.gskinner.com/RegExr/ - using it all the time. But you always have to consider alternatives, before going the Regex way.
The regex that you are looking for is as below:
[^/]+$
where,
^ (caret):Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. Most regex flavors have an option to make the caret match after line breaks (i.e. at the start of a line in a file) as well.
$ (dollar):Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. Most regex flavors have an option to make the dollar match before line breaks (i.e. at the end of a line in a file) as well. Also matches before the very last line break if the string ends with a line break.
+ (plus):Repeats the previous item once or more. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once.
More reference can be found out at this link.
Many Regex softwares and tools are out there. Some of them are:
www.gskinner.com/RegExr/
www.txt2re.com
Rubular- It is not just for Ruby.
This question already has answers here:
Regex search and replace with optional plural
(4 answers)
Closed 6 years ago.
I'm trying to do what should be a simple Regular Expression, where all I want to do is match the singular portion of a word whether or not it has an s on the end. So if I have the following words
test
tests
EDIT: Further examples, I need to this to be possible for many words not just those two
movie
movies
page
pages
time
times
For all of them I need to get the word without the s on the end but I can't find a regular expression that will always grab the first bit without the s on the end and work for both cases.
I've tried the following:
([a-zA-Z]+)([s\b]{0,}) - This returns the full word as the first match in both cases
([a-zA-Z]+?)([s\b]{0,}) - This returns 3 different matching groups for both words
([a-zA-Z]+)([s]?) - This returns the full word as the first match in both cases
([a-zA-Z]+)(s\b) - This works for tests but doesn't match test at all
([a-zA-Z]+)(s\b)? - This returns the full word as the first match in both cases
I've been using http://gskinner.com/RegExr/ for trying out the different regex's.
EDIT: This is for a sublime text snippet, which for those that don't know a snippet in sublime text is a shortcut so that I can type say the name of my database and hit "run snippet" and it will turn it into something like:
$movies= $this->ci->db->get_where("movies", "");
if ($movies->num_rows()) {
foreach ($movies->result() AS $movie) {
}
}
All I need is to turn "movies" into "movie" and auto inserts it into the foreach loop.
Which means I can't just do a find and replace on the text and I only need to take 60 - 70 words into account (it's only running against my own tables, not every word in the english language).
Thanks!
- Tim
Ok I've found a solution:
([a-zA-Z]+?)(s\b|\b)
Works as desired, then you can simply use the first match as the unpluralized version of the word.
Thanks #Jahroy for helping me find it. I added this as answer for future surfers who just want a solution but please check out Jahroy's comment for more in depth information.
For simple plurals, use this:
test(?=s| |$)
For more complex plurals, you're in trouble using regex. For example, this regex
part(y|i)(?=es | )
will return "party" or "parti", but what you do with that I'm not sure
Here's how you can do it with vi or sed:
s/\([A-Za-z]\)[sS]$/\1
That replaces a bunch of letters that end with S with everything but the last letter.
NOTE:
The escape chars (backslashes before the parens) might be different in different contexts.
ALSO:
The \1 (which means the first pattern) may also vary depending on context.
ALSO:
This will only work if your word is the only word on the line.
If your table name is one of many words on the line, you could probably replace the $ (which stands for the end of the line) with a wildcard that represents whitespace or a word boundary (these differ based on context).
I got a string like this:
PREFIX-('STRING WITH SPACES TO REPLACE')
and i need this:
PREFIX-('STRING_WITH_SPACES_TO_REPLACE')
I'm using Notepad++ for the Regex Search and Replace, but i'm shure every other Editor capable of regex replacements can do it to.
I'm using:
PREFIX-\('(.*)(\s)(.*)'\)
for search and
PREFIX-('\1_\3')
for replace
but that replaces only one space from the string.
The regex search feature in Notepad++ is very, very weak. The only way I can see to do this in NPP is to manually select the part of the text you want to work on, then do a standard find/replace with the In selection box checked.
Alternatively, you can run the document through an external script, or you can get a better editor. EditPad Pro has the best regex support I've ever seen in an editor. It's not free, but it's worth paying for. In EPP all I had to do was this:
search: ((?:PREFIX-\('|\G)[^\s']+)\s+
replace: $1_
EDIT: \G matches the position where the previous match ended, or the beginning of the input if there was no previous match. In other words, the first time you apply the regex, \G acts like \A. You can prevent that by adding a negative lookahead, like so:
((?:PREFIX-\('|(?!\A)\G)[^\s']+)\s+
If you want to prevent a match at the very beginning of the text no matter what it starts with, you can move the lookahead outside the group:
(?!\A)((?:PREFIX-\('|\G)[^\s']+)\s+
And, just in case you were wondering, a lookbehind will work just as well as a lookahead:
((?:PREFIX-\('|(?<!\A)\G)[^\s']+)\s+
You have to keep matching from the beggining of the string untill you can match no more.
find /(PREFIX-\('[^\s']*)\s([^']*'\))/
replace $1_$2
like: while (/(PREFIX-\('[^\s']*)\s([^']*'\))/$1_$2/) {}
How about using Replace all for about 20 times? Or until you're sure no string contains more spaces
Due to nature of regex, it's not possible to do this in one step by normal regular expression.
But if I be in your place, I do such replaces in several steps:
find such patterns and mark them with special character
(Like replacing STRING WITH SPACES TO REPLACE with #STRING WITH SPACES TO REPLACE#
Replace #([^#\s]*)\s to #\1_ server times.
Remove markers!
I studied a little the regex tool in Notepad++ because I didn't know their possibilities.
I conclude that they aren't powerful enough to do what you want.
Your are obliged to learn and use a programming language having a real regex capability. There are a number of them. Personnaly, I use Python. It would take 1 mn to do what you want with it
You'd have to run the replace several times for each space but this regex will work
/(?<=PREFIX-\(')([^\s]+)\s+/g
Replace with
\1_ or $1_
See it working at http://refiddle.com/10z