-3

Please consider the following examples.

The next line is **

_This line surrounded with emphasis mark_

`hey this is crazy`

**bold**

Now, I want to figure out a regex that identifies the special characters. Basically I want to use string.replaceAll(regex,"")so that I can replace only these special characters **,_,` from the string. Consider each line to be 1 string.

I can identify that each special character is preceded by either space or a new line, followed by the a string, followed by the special character I am trying to remove.

Also please explain the regex.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Nayanjyoti Deka
  • 369
  • 2
  • 15

1 Answers1

1

You may use

"(?<!\\S)(\\*{2}|[_`])|(\\*{2}|[_`])(?!\\S)"

See the regex demo

The regex matches any **, or _ or ` (with (\*{2}|[_`])) NOT preceded with a non-whitespace symbol (see (?<!\\S)), or any **, _, or ` that is not followed by a non-whitespace symbol.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397