Inverting a regex as a whole - regex

OK, I read through this thread and it hasn't helped me so far.
I have a regex in TextPad (not sure of its engine) like so:
[[:digit:]]+ \= [[:digit:]]+ \+ [[:digit:]]+
that finds a string such as:
1783635 = 1780296 + 3339
and I want to find everything else. I tried encasing the whole expression with [^ expression ] as the TextPad manual says to do, with no luck. I also tried [^][ expression ], ^( expression ), and [^]( expression ), with no luck.
From the thread above, I tried (?! expression ), again, with no luck.
Thoughts?

It is not really possible to match "the opposite of" with regex. Regular expressions must match to succeed, they cannot not match and still succeed.
Depending on your exact situation (and TextPad's regex capabilities), there might be a way around this limitation.
More detail is necessary to say that for sure, though. Please provide a real-world text sample and describe what you want to do with it.

The best way I have found to do this is not with JUST regular expressions, but to use additional functionality from TextPad (bookmarks).
In this case, I needed to identify all lines that did NOT start with PHVS. So I did the following:
Perform "Match All" search with regular expression "^PHVS". This marked every line that started with PHVS
Go to Edit -> Invert Bookmarks. This marked every line that did NOT start with PHVS
Created a macro that pressed F2 (to go to next bookmark) and fix the line the way I needed it
Ran the macro to the end of the file.

Related

Can not find specific regular expression

I can not find a regular expression that matches what I'm looking for.
I would like a regular expression that matches 15 consecutive characters (except space, exclamation point, comma, period). So far the expression is [^!\?.\s!,]{20}. But I do not want that match if in these 15 characters, 10 are identical.
So match with "jqshjsdfhjsdlfdjqlsmskjm" but not with "thaaaaaaaaaaaaaaank"
thank you
You can achieve something close to that: (([^!\?.\s!,])(?!\1)){15}. See the solution working here.
This solution, however, has a setback: it fails when it finds patterns like 131 or bab. If even with this setback the solution works for you, then good. If not, then this is as far as regex goes. You'll have to work out that logic programatically.
Disclaimer: I'm out of time right now and will edit my answer later to include an explanation of the regex and the reason why it has a setback Although someone else could edit this answer and do it for me : ) .

How to replace all lines based on previous lines in Notepad++?

I have an XML code:
<Line1>Matched_text Other_text</Line1>
<Line2>Text_to_replace</Line2>
How to tell Notepad++ to find Matched_text and replace Text_to_replace to Replaced_text? There are several similar blocks of code, with one exactly Matched _text and different Other_text and Text_to_replace. I want to replace all in once.
My idea is to put
Matched_text*<Line2>*</Line2>
in the Find field, and
Matched_text*<Line2>Replaced_text</Line2>
in the Replace field. I know that \1 in regex might be useful, but I don't know where to start.
The actual code is:
<Name>Matched_text, Other_text</Name>
<IsBillable>false</IsBillable>
<Color>-Text_to_replace</Color>
The regex you're looking for is something like the following.
Find: (Matched_text[\w,\s<>\/]*<Color>-).*(</Color>)
Replace: \1Replaced_text\2
Broken down:
`()` is how you tell regex that you want to keep things (for use in /1, /2, etc.), these are called capture groups in regex land.
`Matched_text[\w,\s<>\/]*` means you want your anchor `Matched_text` and everything after it up till the next part of the expression.
`<Color>-).*(</Color>)` Select everything between <Color>- and </Color> for replacement.
If you have any questions about the expression, I highly recommend looking at a regex cheatsheet.

Regular expression everything after a link

I am a complete regular expression idiot, just keep that in mind :)
I am trying to create a regular expression that will match link:xxxxxx where everything after link: is a wildcard.
Can i just do link:* or am I totally misguided?
link:.* should work correctly.
. matches any character, and you want to repeat it "0 to unlimited" times so you add *.
If you're new to regex, a good way to learn it is by using regex101.
For your problem, you can check out this regex101 example
(Note that I have also added the g modifier, which means that you want to select all matches, not just the first matching line)

Vim S&R to remove number from end of InstallShield file

I've got a practical application for a vim regex where I'd like to remove numbers from the end of file location links. For example, if the developer is sloppy and just adds files and doesn't reuse file locations, you'll end up with something awful like this:
PATH_TO_MY_FILES&gt
PATH_TO_MY_FILES1&gt
...
PATH_TO_MY_FILES22&gt
PATH_TO_MY_FILES_ELSEWHERE&gt
PATH_TO_MY_FILES_ELSEWHERE1&gt
...
So all I want to do is to S&R and replace PATH_TO_MY_FILES*\d+ with PATH_TO_MY_FILES* using regex. Obviously I am not doing it quite right, so I was hoping someone here could not spoon feed the answer necessarily, but throw a regex buzzword my way to get me on track.
Here's what I have tried:
:%s\(PATH_TO_MY_FILES\w*\)\(\d+\)&gt:gc
But this doesn't work, i.e. if I just do a vim search on that, it doesn't find anything. However, if I use this:
:%s\(PATH_TO_MY_FILES\w*\)\(\d\)&gt:gc
It will match the string, but the grouping is off, as expected. For example, the string PATH_TO_MY_FILES22 will be grouped as (PATH_TO_MY_FILES2)(2), presumably because the \d only matches the 2, and the \w match includes the first 2.
Question 1: Why doesn't \d+ work?
If I go ahead and use the second string (which is wrong), Vim appears to find a match (even though the grouping is wrong), but then does the replacement incorrectly.
For example, given that we know the \d will only match the last number in the string, I would expect PATH_TO_MY_FILES22&gt to get replaced with PATH_TO_MY_FILES2&gt. However, instead it replaces it with this:
PATH_TO_MY_FILES2PATH_TO_MY_FILES22&gtgt
So basically, it looks like it finds PATH_TO_MY_FILES22&gt, but then replaces only the & with group 1, which is PATH_TO_MY_FILES2.
I tried another regex at Regexr.com to see how it would interpret my grouping, and it looked correct, but maybe a hack around my lack of regex understanding:
(PATH_TO_\D*)(\d*)&gt
This correctly broke my target string into the PATH part and the entire number, so I was happy. But then when I used this in Vim, it found the match, but still replaced only the &.
Question 2: Why is Vim only replacing the &?
Answer 1:
You need to escape the + or it will be taken literally. For example \d\+ works correctly.
Answer 2:
An unescaped & in the replacement portion of a substitution means "the entire matched text". You need to escape it if you want a literal ampersand.

Find / Replace functionality that allows for boundary replacements instead of expressions

Apologies in advance for the confusing title. My issue is as follows, I have the following text in about 600 files:
$_REQUEST['FOO']
I would like to replace it with the following:
$this->input->post('FOO')
To clarify, I am matching against the following:
$_REQUEST any number of A-Za-z\d followed by a ]
and replacing it with:
$this->input->post( the alphanumeric word from above followed by a )
Or in general:
Anchor token TEXT TO KEEP end anchor token
This differs from standard find/replace as I want to retain text inside of two word boundaries.
Is this functionality present in any text editors (Eclipse,np++,etc). Or am I going to need to write some type of program to parse these 600 files to make the replacement?
s/\$__REQUEST\[(.*?)]/$this->input->post(\1)/
The .*? will match everything from [ to the first ] rather than the last although it's unlikely that it will matter in this case.
By the way the PHP superglobal is $_REQUEST rather than $__REQUEST
You can do this in Notepad++ using regular expressions. Replace
\$_REQUEST\['([^']*)'\]
with
$this->input->post('$1')
If you ever have double-quotes too, you can do use a more complex expression to handle both cases, though I'm not sure Notepad++ supports backreferences; replace
\$_REQUEST\[(['"])(.*?)\1\]
with
$this->input->post($1$2$1)
Note that I've reverted to using #ExplosionPills' suggested (.*?) hereā€”it may be better, actually.