Expression to use find in the replace

A place to try and solve your RegEx problems.
Post Reply
User avatar
Gerald

Expression to use find in the replace

Post by Gerald »

I'd like to find "DSS 694" and replace it with "{Name:694}694". My problem is that I want this to occur for multiple numbers (i.e. 695,696, etc), with one RegEx. Is this possible? Thanks for your help!
User avatar
Gerald

Post by Gerald »

Holy crap, I figured it out! It's called a Capture group. Here is what worked for me: Find "DSS (...)" Replace: "{Name:$1}\\n$1"

Hopefully this helps someone else. :)
User avatar
Fool4UAnyway

Post by Fool4UAnyway »

Perhaps it is not necessary in this case, but you might want to be more specific in your regular expression, limiting the part after DSS to only numbers.

Find: "DSS (\d{3})"

\d (digit) = [0-9] meaning the character range from 0 to 9.

If your numbers were hexadecimal you might want to use the character range [0-9A-Z] or [0-9A-Za-Z] if your search is set to be case-sensitive.
User avatar
Fool4UAnyway

Post by Fool4UAnyway »

The {3} means: a consecutive range of exactly 3.

You can allow varying lengths by specifying the lower and/or upper length boundaries:

{2,4} = 2 to 4 times (digits)
{2,} = at least 2 times (digits)

Find out a lot more about regular expressions at:

http://www.regular-expression.info

repetition (numbers):

http://www.regular-expressions.info/quickstart.html

{} quantifiers:

http://www.regular-expressions.info/repeat.html
Post Reply