Home > Net >  Add Find Special Character at beginning and ADD to END of string with regex
Add Find Special Character at beginning and ADD to END of string with regex

Time:05-25

Have a string that starts with a # symbol and would like to add the same # symbol.

The string could contain any type of lower/upper, numbers, comas, periods, etc. Each string is a single separate line.

Here is what I have tried with no success:

Find:       (?=#)([\S\s] )           # www.admitme.app
Find:       (?=#\B)([\S\s] )         # Carlo Baxter
Find:       (?=#\B)([A-Za-z0-9] )    # resumes in 15 minutes

Replace:    $1 #                     # resumes in 15 minutes #

Yes I'm a noob with regex... Thanks in advance Hank K

CodePudding user response:

The following pattern is working in your demo:

(?=#\B)(.*)

This works in multiline mode, because then the .* will not match across newlines. You were using [\s\S]*, which will match across newlines, even in multiline mode. Here is the updated demo.

CodePudding user response:

You can do the same replacement without lookarounds or capture groups using one of these patterns. The point is to match any character without newlines using .* (And not have a flag set to have the dot matches newlines)

#\B.*
# .*

In the replacement use the matched text followed by a space and #

$0 #

See a regex demo.

  • Related