Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression to add characters before and after numbers

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.

like image 550
Mike Avatar asked Dec 24 '11 10:12

Mike


People also ask

How do you add a character in regex?

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).

What does ?= Mean in regular expression?

?= 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).

What does \b mean in regular expressions?

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.)

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


1 Answers

Regular expression:

Find regex = \[\d+\]
Replace regex = xxxxxxxxx$&xxxxxxxxx


Refer: regexr

like image 138
Mukul Aggarwal Avatar answered Oct 19 '22 14:10

Mukul Aggarwal