Pv66
Pv66

Reputation: 155

How to match new-line character using regex

I'm trying to match the special characters and the breaks using a regex to cleanse the string so that I'm left with only the following string 'All release related activities' extracted from the following line:

{"Well":"\n\n\t\n\t\n\n\t\n\tAll release related activities\n\n\t"}

I've tried the regex ^{.Well":" and I'm able to match till the first colon appears. How do I match the \n characters in the string?

Upvotes: 1

Views: 1361

Answers (2)

Booboo
Booboo

Reputation: 44043

Try:

/":"(?:\\[nt])*(.*)}"$/

See Regex Demo

  1. ":" Matches ":".
  2. (?:\\[nt])* Matches 0 or more occurrences of either \n or \t.
  3. (.*) Matches 0 or more characters in Capture Group 1 until:
  4. }"$ Matches }" followed by the end of the string.

The string you are looking for is in Capture Group 1.

Upvotes: 1

Mustofa Rizwan
Mustofa Rizwan

Reputation: 10466

I am not quite sure about the prefix of "well:" So I am basically providing you with a basic regex:

^\{[^}]*?(?:\\[ntr])+([^}]+)\}

and replace by:

\1

Example

Upvotes: 1

Related Questions