I thought my regex skills were strong, but I'm getting crushed.
I have several lines in thsi format
Time: 105 0 0
Time: 88 0 1
Time: 44 1 1
Time: 64 1 0
I want theses to turn into this:
Time: 105 thread00
Time: 88 thread01
Time: 44 thread11
Time: 64 thread10
I can match the [0-9][ ][0-9] section... I match it with that regex right there!
But I don't know how to preserve the values AND remove the space. Replacing it wholesale with new stuff, sure... but how do I PRESERVE values?
Find what: (\d)\s(\d)$
Replace with: thread\1\2
\d matches any digit, \s matches any space character.
The parentheses will be captured for use as \1, \2, \3... and \0 will provide the entire match.*
$ matches the end of a line, so that you don't accidentally match the "5 0" in the first line.
*Note that some regex engines use the \1 pattern while some others will use $1. Notepad++ uses the former.
You can try this:
Pattern:
/^(.*)(\d+)\s(\d+)$/
Breakdown:
^ # start of line
(.*) # the first part of the line -- capture $1
(\d+) # the first number (1 or more) -- capture $2
\s # the space between the numbers
(\d+) # the second number (1 or more) -- capture $3
$ # end of line
Replace:
/$1thread$2$3/
Result:
Time: 105 thread00
Time: 88 thread01
Time: 44 thread11
Time: 64 thread10
Demo: http://regex101.com/r/gB8uS4
Related
If I have the following lines, how to
leave only 10-11 digits long strings that starts with "04" or "05". Don't remove empty lines.
0409999999 012345678
012345678 0409999999
023456789 034566 0455555555
012345678 012345678
0299999999
so the lines above should then look like:
0409999999
0409999999
0455555555
I would suggest pattern
\b\d{0,9}|0[45]\d{8,9}\b
Explanation:
\b - word boundary
\d{0,9} - match up to 9 digits
| - alternation
0 - match 0 literally
[45] - match 4 or 5
Regex demo
EDIT
After update you can use [ \t]*(?!0[45]\d{8,9})\b\d+[ \t]*
The difference here is that it uses negative lookahead to assure that what is ahead is not 10-11 digit number starting with 04 or 05.
[ \t] are used to trim space and tabs.
Then you just need to replace it with empty string.
\b(?!04|05)\d{1,9}\b|\b\d{10,11}\b
I'm looking for a regex that matches housenumbers combined with additions for all addresses below:
Breestraat 4
Breestraat 45
Breestraat 456
Dubbele Straat 4a
Dubbele Straat 4-a
5 meistraat 1a
5meistraat 12
5meistraat 12a
Teststraat 22-III
Now the following regex works, except in the first case. This is because the single digit housenummber is missed because of the first \d in the regex (which prevents a starting digit to be captured).
\d?.(\d+.+)$
regex to extract housenumber addition
I'm scratching my head how to get the housenumer '4' for the first line. so basically how to change the "skip starting digit" to "skip starting digit but let it have to result on the capturing group".
You can use
\d+\D*$
\d+\S*$
See the regex demo #1 and regex demo #2.
The pattern matches
\d+ - one or more digits
\D* - zero or more non-digit chars
\S* - zero or more non-whitespace chars
$ - end of string.
It's not perfectly clear what you are requesting precisely..
Anyway this is the pattern matching the house number at the end of the string:
\d+[-\da-zI]*$
https://regexr.com/6l0g7
Anyway I'm aware this is not a valid answer
I have strings like is below,
nn"h11p3ppppvxq3b288N1 m 227"]
{vanxtageendganmesbhorgtgt(1702)}' d3zd6xf8dz8xd6dz8f6zd8`
[nn"5rvh11p3ppppvxq3b288N1 n 227"]
{vanxtageendganmesbhorgtgt(1802)}
d3zd6xf8dz8xd6dz8f6zd8
I start my 1st capturing group from m 227 till end of third line,
And my 2nd group from n 227 till end of third line .....
Now I want to add some digits to end of first captured group , say it -22
And some digits to end of second captured group, say it -11
My first regex can match and works separately so 2nd as well .... but to make them combine with | it doesn't .....
Search: (m\s.*\n.*\n.*)
Replace: $1 -22
My combined regex is as below
(m\s.*\n.*\n.*|n\s.*\n.*\n.*)
Replace: $1-22 $2-11
But this will add (-22 -11) to both intendeds ...
I want the output to be as below
nn"h11p3ppppvxq3b288N1 m 227"]
{vanxtageendganmesbhorgtgt(1702)}
d3zd6xf8dz8xd6dz8f6zd8 -22
[nn"5rvh11p3ppppvxq3b288N1 n 227"]
{vanxtageendganmesbhorgtgt(1802)}
d3zd6xf8dz8xd6dz8f6zd8 -11
I have used | or for to combine both regexes to works as one for the purpose of time Savage ....
Any help will be appreciated
You can use
Find What: ([mn])\s.*\R.*\R.*
Replace With: $& -$1
Details:
([mn]) - Group 1 ($1): m ior n
\s - a whitespace
.*\R.*\R.* - a line, a line break, then again a line and a line break and then a line.
The $& in the replacement is the backreference to the whole match.
I have a file with the following lines (condensed example, real file is 1.000+ lines):
...
type1.value1=60 <-- replace 60 with 72 from line 5
type1.value2=15 <-- replace 15 with 14 from line 6
type2.value1=50 <-- replace 50 with 72 from line 5
type2.value2=18 <-- replace 18 with 14 from line 6
type3.value1=72
type3.value2=14
...
I want to replace all values from type(x) with the values from type3. There are many type/value combinations, so i would like to avoid handwork. Also, i have to do this really often.
Is that possible with Notepad++ Regex find/replace?
The matching expression is the following, where the first group should stay the same and the second should be replaced by the result of yet anoter regex.
^type1.([\w]+)=([\S]+)
Regex:
type(?!3\.)\d+\.value(\d+)=\K\d+(?=[\s\S]*?type3\.value\1=(\d+))
Replace with:
\2
Explanation:
type(?!3\.)\d+ Match a type other than 3
\.value(\d+)= Match every thing up to = but capture digits
\K Forget matches up to now
\d+ Match following digits
(?= Start of positive lookahead
[\s\S]*? Match anything lazily
type3\.value\1= Up to the same value of type3
(\d+) Then capture its value in CP #2
) End of positive lookahead
Live demo
The point is matching valueX from a type different than 3 then look for the same valueX from type3. If valueX is hypothetical or there isn't anything special to be looked, then there is no pure approach using regex in a find / replace functionality.
I've got a document that looks something like this:
# Document ID 8934
# Last updated 2018-05-06
52 84 12 70 23 2 7 20 1 5
4 2 7 81 32 98 2 0 77 6
(..and so on..)
In other words, it starts off with a few comment lines, then the rest of the document is just a bunch of numbers separated by spaces.
I'm trying to write a regex that gets all digits on all lines that don't start with #, but I can't seem to get it.
I've read over answers such as
Regular Expressions: Is there an AND operator?
Regex: Find a character anywhere in a document but only on lines that begin with a specific word
and pawed through sites such as http://regular-expressions.info, but I still can't get an expression that works (the best I can get is a lengthy version of ^[^#].*
So how can I match digits (or text, or whatever) in a string, but only on lines that don't start with a certain character?
Your regex ^[^#].* uses a negated character class which matches not a # from the start of the string ^ and after that matches any character zero or more times.
This would for example also match t test
What you might do is use an alternation to match a whole line ^#.*$ that starts with a # or capture in a group one or more digits (\d+)
Your digits are captured group 1. You could change the (\d+) to for example a character class ([\w+.]+) to match more than only digits.
(?:^#.*$|(\d+))
Details
(?: Non capturing group
^#.*$ Match from the start of the line ^ a # followed by any character zero or more times .* until the end of the string $
| Or
(\d+) capture one or more digits in a group
) Close non capturing group
I think a way simpler method would be to replace the lines with "" first with this regex:
^#.*
And then you can just match all the numbers with this:
-?\d+ (-? is for negative)