I have some code to reform (1000 lines) and I want to go from this
if $one=0 and $two=32 then $dist=1
if $one=0 and $two=15 then $dist=2
if $one=0 and $two=19 then $dist=3
To this
if $one=0 and $dist=1 then $two=32
if $one=0 and $dist=2 then $two=15
if $one=0 and $dist=3 then $two=19
In a few words swap both $two and its value with $dist and its value.
Can it happen with regular expressions of notepad+?
I tried:
if ([^ ]+) and ([^]+) then ([^]+)
Ctrl+H
Find what: (\$two=\d+)( then )(\$dist=\d+)
Replace with: $3$2$1
Replace all
Explanation:
(\$two=\d+) : group 1, contains "$two=1 or more digits"
(\s+then\s+) : group 2, literally "then" surrounded by spaces
(\$dist=\d+) : group 3, contains "$dist=1 or more digits"
\$ must be escaped because it is a special character in regex.
Replacement:
$3$2$1 : group 3 group 2 group 1
Result for given example:
if $one=0 and $dist=1 then $two=32
if $one=0 and $dist=2 then $two=15
if $one=0 and $dist=3 then $two=19
Related
in nodejs, I have to following pattern
(/^(>|<|>=|<=|!=|%)?[a-z0-9 ]+?(%)*$/i
to match only alphanumeric strings, with optional suffix and prefix with some special characters. And its working just fine.
Now I want to match the last '%' only if the first character is alphanumeric (case insensitive) or also a %. Then its optionally allowed, otherwise it should not match.
Example:
Should match:
>test
!=test
<test
>=test
<=test
%test
test
%test%
test%
Example which should not match:
<test% <-- its now matching, which is not correct
<test< <-- its now **not** matching, which is correct
Any Ideas?
You can add a negative lookahead after ^ like
/^(?![^a-z\d%].*%$)(?:[><]=?|!=|%)?[a-z\d ]+%*$/i
^^^^^^^^^^^^^^^^^
See the regex demo. Details:
^ - start of string
(?![^a-z\d%].*%$) - fail the match if there is a char other than alphanumeric or % at the start and % at the end
(?:[><]=?|!=|%)? - optionally match <, >, <=, >=, != or %
[a-z\d ]+ - one or more alphanumeric or space chars
%* - zero or more % chars
$ - end of string
You might use an alternation | to match either one of the options.
^(?:[a-z0-9%](?:[a-z0-9 ]*%)?|(?:[<>]=?|!=|%)?[a-z0-9 ]+)$
^ Start of string
(?: Non capture group
[a-z0-9%] Match one of the listed in the character class
(?:[a-z0-9 ]*%)? Optionally match repeating 0+ times any of the character class followed by %
| Or
(?:[<>]=?|!=|%)? Optionally match one of the alternatives
[a-z0-9 ]+ Match 1+ times any of the character class
) Close non capture group
$ End of string
Regex demo
line A
foo bar bar foo bar foo
line B
foo bar bar foo
In line A, there are multiple occurrence of double space.
I only want to match lines like line B which has only once double space occurrence.
I tried
^.*\s{2}.*$
but it will match both.
How may I have the desired output? Thank you.
If you wish to match strings that contain no more than one string of two or more spaces between words you could use following regular expression.
r'^(?!(?:.*(?<! ) {2,}(?! )){2})'
Start your engine!
Note that this expression matches
abc de fgh
where there are four spaces between 'c' and 'd'.
Python's regex engine performs the following operations.
^
(?! : begin negative lookahead
(?: : begin non-capture group
.* : match 0+ characters other than line terminators
(?<! : begin negative lookbehind
[ ]{2,} : match 2+ spaces
(?! ) : negative lookahead asserts match is not followed by a space
) : end negative lookbehind
) : end non-capture group
{2} : execute non-capture group twice
) : end negative lookahead
You can do:
^(?!.*[ \t]{2,}.*[ \t]{2,})
# Negative look ahead assertion that states 'only start the match
# on this line IF there are NOT 2 (or potentially more) breaks with
# two (or potentially more) of tabs or spaces'.
Demo 1
If you want to require ONE double space in the line but not more:
^(?=.*[ \t]{2,})(?!.*[ \t]{2,}.*[ \t]{2,})
# Positive look ahead that states 'only start this match if there is
# at least one break with two tabs or spaces'
# BUT
# Negative look ahead assertion that states 'only start the match
# on this line IF there are NOT 2 (or potentially more) breaks with
# two (or potentially more) of tabs or spaces'.
Demo 2
If you want to limit to only two spaces (not tabs and not more than 2 spaces):
^(?=.*[ ]{2})(?!.*[ ]{2}.*[ ]{2})
# Same as above but remove the tabs as part of the assertion
Demo 3
Note: In your regex you have \s as the class for a space. That also matches [\r\n\t\f\v ] so both horizontal and vertical space characters.
Note 2:
You can do this without a regex as well (assuming you only want lines that have 1 and only 1 double space in them):
txt='''\
line A
foo bar bar foo bar foo
line B
foo bar bar foo'''
>>> [line for line in txt.splitlines() if len(line.split(' '))==2]
['foo bar bar foo']
You can get the match without lookarounds by starting the match with 1+ non whitespace chars.
Then optionally repeat a single whitespace char followed by non whitespace chars before and after matching a double whitespace char.
The negated character class [^\S\r\n] will match any whitespace chars except a newline or carriage return. If you want to allow matching newlines as well, you could use \s
^\S+(?:[^\S\r\n]\S+)*[^\S\r\n]{2}(?:\S+[^\S\r\n])*\S+$
Explanation
^ Start of string
\S+ Match 1+ non whitespace chars
(?: Non capture group
[^\S\r\n]\S+ Match a whitespace char without a newline
)* Close group and repeat 0+ times
[^\S\r\n]{2} Match the 2 whitespace chars without a newline
(?: Non capture group
\S+[^\S\r\n] Match 1+ non whitespace chars followed by a whitespace char without a newline
)* Close group a and repeat 1+ times
\S+ Match 1+ non whitespace chars
$ End of string
Regex demo
So, I've seen that you can remove between two characters and remove between two strings but I haven't been able to find a system that works between a string and a character.
I need to remove the numbers between the two brackets in...
provinces= {
923 6862 9794 9904 11751 11846 11882
}
Keep in mind that these files also contains other brackets which are needed. I've looked around for a solution for this but none seem to work :/
Thanks for the help.
This one will do the job:
Ctrl+H
Find what: \b(provinces\s*=\s*\{)[^}]+(\})
Replace with: $1$2
Replace all
Explanation:
\b : a word boundary
( : start group 1
provinces : literally "provinces"
\s* : 0 or more spaces
= : equal sign
\s* : 0 or more spaces
\{ : an open curly bracket, must be escaped because it has special meaning in regex
) : end group 1
[^}]+ : any character that is not a close curly bracket
(\}) : group 2, a close curly bracket, escaped.
Replacement:
$1$2 : group 1 then group 2
I need to clear text around other text... example:text = hello there sir how are you doing
Tex to be removed = hello sir are doing
Result = there how you
What would i put in to skip all other text but the words in the text to be removed with the ctrl h menu.
You may use a regex based search and replace:
Find what: \S+(?:\s+(\S+))?
Replace with: $1
See the regex demo
Details:
\S+ - one or more chars other than whitespace
(?:\s+(\S+))? - one or zero occurrences of
\s+ - 1+ whitespaces
(\S+) - Group 1: one or more chars other than whitespace
You can do find/replace with regexes in Notepad++.
How could I use string.gmatch(text, pattern) to do this:
text = "Hello.%23 Awesome7^.."
pattern = --what to put here?
for word in string.gmatch(text, pattern) do
print(word)
end
--Result
>test
Hello.%23
Awesome7^..
>
I have been using "%w+%p", but this results in:
>test
Hello.
%
23
Awesome7^
.
.
Which is not the desired result.
Note: I have not tested this exact string, it could vary... but still, does not create the desired result
From your example, every word contains no spaces, and are separated by spaces, so the simplest pattern is "%S+":
text = "Hello.%23 Awesome7^.."
pattern = "%S+"
for word in string.gmatch(text, pattern) do
print(word)
end
"%s" matches a space character, "%S" matches a non-space character.