What is the regexpression for fixed words and a variable - regex

I am using sublime to search and replace text in a code.
This is what I want to find
pre + <anyvariable> + post
and then replace it like this
sanitize(<anyvariable>)
I don't know how to come up with regexp after finding "pre + "
Came up with something like this
/(\pre \+)(?=.*)/g

Try this:
/^(.*\+)\s*(\S+)\s*(\+.*)$/
This will give you everything in three capture groups, starting from the beginning of the line between the first and second +, and then everything following to the end of the line. If there's other content such as more +'s, then this probably won't work blanketly.

Without a computer, and not sure a out what you want, but if you search for:
pre \+ (.*?) \+ post
And replace with:
sanityze($1) (or \1 I never remember)
It should do what you want :-)
If you want to be able to have linebreaks, replace .* by (?:.|\n)

Related

Notepad ++ regex. Finding and replacing with wildcard, but without allowing any spaces?

I have something like this in txt
[[asdfg]] [[abcd|qwerty]]
in a row, but I want it to look like that
[[asdfg]] [[qwerty]]
using wildcards ( [[.*\| ) when trying to search, results in it finding the whole line up to the "|" Not allowing it to have a space in between should work, but I don't know how to do that.
Edit 1
It's from a wikipedia dump, so the first part is the word in it's basic form and the second is how it fits into the sentence. Something like [[I]] [[be|was]] [[at]] [[the]] [[doctor]] And I want to change it into normal sentences
[[I]] [[was]] [[at]] [[the]] [[doctor]]
Edit 2
I found somewhat of a solution. I just put every word in a new line, did the first regex and then deleted newlines. That did kinda mess up my spacing though...
Try this regex:
\[\[\w+\|(\w+)\]\]
Replace with:
[[$1]]
Make sure you choose Regular expression at the bottom before you click Replace All in Notepad++.
You can do it all in one run like so
\[{2}(?:(?!\]{2}).)+?\|([^\]]+)
This needs to be replaced by
[[$1
See a demo on regex101.com.
Broken down this says:
\[{2} # match [[
(?:(?!\]{2}).)+? # do not overrun ]]
\| # |
([^\]]+) # capture anything not ] into group 1
Afterwards, you'll only need to replace the open brackets and the content of group $1

find a single quote at the end of a line starting with "mySqlQueryToArray"

I'm trying to use regex to find single quotes (so I can turn them all into double quotes) anywhere in a line that starts with mySqlQueryToArray (a function that makes a query to a SQL DB). I'm doing the regex in Sublime Text 3 which I'm pretty sure uses Perl Regex. I would like to have my regex match with every single quote in a line so for example I might have the line:
mySqlQueryToArray($con, "SELECT * FROM Template WHERE Name='$name'");
I want the regex to match in that line both of the quotes around $name but no other characters in that line. I've been trying to use (?<=mySqlQueryToArray.*)' but it tells me that the look behind assertion is invalid. I also tried (?<=mySqlQueryToArray)(?<=.*)' but that's also invalid. Can someone guide me to a regex that will accomplish what I need?
To find any number of single quotes in a line starting with your keyword you can use the \G anchor ("end of last match") by replacing:
(^\h*mySqlQueryToArray|(?!^)\G)([^\n\r']*)'
With \1\2<replacement>: see demo here.
Explanation
( ^\h*mySqlQueryToArray # beginning of line: check the keyword is here
| (?!^)\G ) # if not at the BOL, check we did match sth on this line
( [^\n\r']* ) ' # capture everything until the next single quote
The general idea is to match everything until the next single quote with ([^\n\r']*)' in order to replace it with \2<replacement>, but do so only if this everything is:
right after the beginning keyword (^mySqlQueryToArray), or
after the end of the last match ((?!^)\G): in that case we know we have the keyword and are on a relevant line.
\h* accounts for any started indent, as suggested by Xælias (\h being shortcut for any kind of horizontal whitespace).
https://stackoverflow.com/a/25331428/3933728 is a better answer.
I'm not good enough with RegEx nor ST to do this in one step. But I can do it in two:
1/ Search for all mySqlQueryToArray strings
Open the search panel: ⌘F or Find->Find...
Make sure you have the Regex (.* ) button selected (bottom left) and the wrap selector (all other should be off)
Search for: ^\s*mySqlQueryToArray.*$
^ beginning of line
\s* any indentation
mySqlQueryToArray your call
.* whatever is behind
$ end of line
Click on Find All
This will select every occurrence of what you want to modify.
2/ Enter the replace mode
⌥⌘F or Find->Replace...
This time, make sure that wrap, Regex AND In selection are active .
Them search for '([^']*)' and replace with "\1".
' are your single quotes
(...) si the capturing block, referenced by \1 in the replace field
[^']* is for any character that is not a single quote, repeated
Then hit Replace All
I know this is a little more complex that the other answer, but this one tackles cases where your line would contain several single-quoted string. Like this:
mySqlQueryToArray($con, "SELECT * FROM Template WHERE Name='$name' and Value='1234'");
If this is too much, I guess something like find: (?<=mySqlQueryToArray)(.*?)'([^']*)'(.*?) and replace it with \1"\2"\3 will be enough.
You can use a regex like this:
(mySqlQueryToArray.*?)'(.*?)'(.*)
Working demo
Check the substitution section.
You can use \K, see this regex:
mySqlQueryToArray[^']*\K'(.*?)'
Here is a regex demo.

notepad++ regex replace word in first line

Im trying to use the following regex to search and replace in multiple files in notepad++
([^\n]*)(state="1")([^\n]*)*.
This searches and finds state="1" in the first line of each file and works fine.
However, when I try to replace state="1" using:
Replace with: $1 state="5"
it cuts off the rest of the line.
I thought that it might be possible to get the rest of the line using:
Replace with: $1 state="5" $2
However, $2 doesnt seem to exist as a variable.
Is there some way to attach the rest of the line into variable $2?
Cheers
Heres an image to show how
(?=\A[^\n]*)state="1"
is not working
Ive updated my version of notepad++ and everything
Each capture group, (…), is assigned a number, so $2 represents the second capture group, (state="1"). The remainder of the line is captured in $3.
Either remove the capture group around state="1":
([^\n]*)state="1"([^\n]*)*.
Or use $3:
Replace with: $1 state="5" $3
Also, given the simplicity of the task, I don't see why you couldn't just search for state="1" and replace with state="5". There doesn't seem to be any need for regular expressions here.
Update There's nothing in the pattern listed so far which limits the result to only matching strings on the first line. If you need that I'd recommend using a pattern like this:
(?=\A[^\n]*)state="1"
With these settings:
Update There seems to be some strange behavior with the \A (beginning of text) anchor inside the lookbehind. Removing from the lookbehind seems to work. Try this pattern:
\A([^\n]*)state="1"
And replace with:
$1state="5"
All the other settings should be fine.

Regular Expression Replace, can't insert simple character

I have tried everything possible and 2 hours trying to get this to work.
I'm using the search and replace option in Code (version 1) but just can't get the replace code working.
I want to add a semi colon in between some characters. I search like this:
)}[a-z]
..and that works fine.
and I've tried all of these, but not one keeps the [a-z] part, they all remove/replace it.
)};\1
)};*1
)};[a-z]
)};\1\2
)};$1
)};($1)
)};(\1)
)};[$1]
)};[\1]
I've set it to use the POSIX Basic option, but any option, setting will do. I just need it to simply insert a semicolon
This should work:
s/\)\}([a-z])/)};\1/g
In essence, we literally match a close parenthesis followed by a close brace. We then match a letter between a and z, but the parentheses around the [a-z] make that a capture group that we can reference. We can then (and only then) use \1 in the replacement. Without a capture group, it doesn't know what \1 refers to. With one, it does.
Using it with sed:
% echo 'hello)}world' | sed -Ee 's/\)\}([a-z])/)};\1/g'
hello)};world

Matching all occurrences of a html element attribute in notepad++ regex

I have a file which has hundreds of links like this:
<h3>aspnet</h3>
Ex 1
Ex 2
Ex 3
So I want to remove all the elements
icon="data:image/png;base64,ivborw0kggoaaaansuheugaaabaaaaaqcayaaaaf8..."
from all the lines. I went through the official Notepad++ regex wiki and have come up with this after several trials:
icon=\"[^\.]+\"
The problem with this is, it is selecting past the second double quote and stopping at the next occurring double quote. To illustrate, this will select the following content:
icon="data:image/png;base64,...jbvebich4sec9zgth1sfue1cdt...">EX 1</a> <a href="
If I modify the above regex to,
icon=\"[^\.]+\">
Then it is almost perfect, but it is also selecting the >:
icon="data:image/png;base64,...jbvebich4sec9zgth1sfue1cdt...">
The regex I am looking for would select like this:
icon="data:image/png;base64,...jbvebich4sec9zgth1sfue1cdt..."
I also tried the following, but it doesn't match anything at all
icon=\"[^\.]+\"$
Just match anything but a quote, followed by a quote:
icon="[^"]+"
Just tested with notepad++ 6.2.2 and confirmed that this matches correctly as written.
Broken down:
icon="
This is fairly obvious, match the literal text icon=".
[^"]+
This means to match any character that is not a ". Adding the + after it means "one or more times."
Finally we match another literal ".
I am not a notepad++ user. so don't know how notepad++ plays with regex, but can you try to replace
icon=\"[^>]* to (empty string) ?
Try this solution:
This is I just check was working as you wanted it.
The way achieving your goal:
Find what: (icon.*")|.*?
Replace with: $1