1

In a regular expression (notepad++), I want to search for:( )|(:)|(_)|(\.), and to insert \ before to, as above, a blank space, colon, under line and ".".

Search example: abcd:1234 jiod.8ufd_adfd

Result: abcd\:1234\ jiod\.8ufd\_adfd

Briefly, how can I refer to what was found in the replace expression?

Note that it is not \1, \2, \3 or \4 in the example, as I need to include what was found, there is no way to know which was found, is there?

2
  • Do you mean you want to add a backslash before each colon, space dot or underscore? match [:\h._] and replace with a backslash and the full match \\$& regex101.com/r/Si6rHp/1 Commented Aug 17, 2020 at 15:41
  • Did the answer work out? Commented Dec 20, 2020 at 14:00

2 Answers 2

2

You can use a single character class (instead of using the alternation with capturing groups) to match one of the listed

In the replacement use $& to refer to the matched text and prepend a backslash.

Match

[:\h._]

Replace with

\\$&

The character class matches either a colon, horizontal whitespace char, dot or underscore.

Regex demo

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

There's no such thing as insert, because if you think about it, inserting is just replacing the original with a new string that contains the old text as well.

Try this instead: search for ([ :_.]) (your original regex is pointlessly complicated) and replace with \\$1 (ie, slash followed by the original text).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.