I have a list of numbers between square brackets, and I need to add words before and after the exact numbers (i.e. keep the same numbers). I use notepad++ to replace, but if you have a solution with other program please advise.
Example:
text [121] othertext
moretext [16] othertextmore
andtext [5940] othertextplus
outcome:
text xxxxxxxxx [121] xxxxxxxxx othertext
moretext xxxxxxxxx [16] xxxxxxxxx othertextmore
andtext xxxxxxxxx [5940] xxxxxxxxx othertextplus
The numbers are of course \d+
but I want to tell it to keep the same numbers when looking.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).
Most of the standard escapes supported by Python string literals are also accepted by the regular expression parser: \a \b \f \n \N \r \t \u \U \v \x \\ (Note that \b is used to represent word boundaries, and means “backspace” only inside character classes.)
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
Regular expression:
Find regex = \[\d+\]
Replace regex = xxxxxxxxx$&xxxxxxxxx
Refer: regexr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With