I am trying to use ant to replace the first occurrence of a regex from the bottom of a file.
I am presently using replaceregexp to search and replace, but can't find any way
to make it work from the end of a file.
Does anyone have any suggestions to solve this?
Thanks,
Chaitanya
Regex works from left to right (from top to bottom), but you can use a negative lookahead to assure that a match is the last one. The idea is that you only match something if the match isn't present afterwards.
If you want to match word (in any context, including crossword), word(?![\s\S]*word) will match only the last occurrence.
Related
With the string ant apple bat I want to select only ant and bat without selecting apple or any white space.
I've tried a few different things like;
\bant\b\bbat\b
or
ant\bbbat\b
or
(\b(ant)\b.*\b(bat)\b)
or
ant(?:.*)bat
or
ant(?:\sapple\s)bat
or
(ant)*[^a-z\s]*(bat)
I know the above examples are stupid but i'm just messing around on a regex tester trying to figure it out. I did regex before and I figured it out but can't seem to remember how i did it. Seems pretty simple just to select "ant" and "bat" but obviously not. Is there some way to deselect a match or to remove part of the match?
Use a non capturing group with alternation:
(?:\bant\b|\bbat\b)
I guess you've understood it wrong. A single regex match will match only contiguous text. A simple regex like ant|bat with either match ant or bat. But if you want to match ant and bat ignoring the text in between would not work.
When using regex with any programming languages like python or java, you would loop through the multiple matches and perform the action accordingly.
If you just want two different words, that looks like this:
\b(ant|bat)\b
The \b is a word boundary
The parenthesis is a group
The pipe | is an or statement
a word boundary
followed by either the word ant or the word bat
ending in a word boundary
A word boundary is probably what you would expect it to be, whitespace, end of a line, punctuation
I want to find all image: in a .yaml file, and replace the first match with image:1.jpg, and second image:2.jpg, etc. To do this, I think I need to find out the indexes of regex matches and replace them accordingly. Is this possible? Can I do it in Atom? Besides, is there any other better way to do this?
First, install increment-selection: https://atom.io/packages/increment-selection
Then, enable regex search and do a search for "image:.*" and replace with "image:aaaaa.jpg"
Then do a find all on "aaaaa" and hit ctrl-shift-i to replace that with incrementing numbers. You should end up with image:1.jpg, image:2.jpg and so on.
I'm trying to find a regex for removing everything after the last dot in a file. So far I've found ways to remove all text before the first dot, but I can't seem to find a way to select the end of the file. Could you help me on the way?
Thanks in advance
You can try something like:
\.[^.]*$
to match everything including and after the last dot. If you don't want to include the last dot, then you can use a positive lookbehind:
(?<=\.)[^.]*$
Try following regex for search and replace
s/\.[^.]*$/\./
On Bigquery, r'([^.]+).?$' works, if you want to remove the last dot.
I need help regarding using negative lookahead. I am using Notepad++ and I want to delete all lines except the lines that contain <title>(.*)</title>
I tried a couple of things but that didnt work.
^.*(?!<title>).*</title>
^.*(?!<title>.*</title>)
You are close:
^(?!.*<title>.*</title>).*
By this regex ^.*(?!<title>.*</title>), the regex engine will just find some position that it cannot find <title>.*</title> (end of line is one such valid position).
You need to make sure that, from the start of the line, there is no way you can find <title>.*</title> anywhere in the line. That is what my regex does.
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.