zerohedge
zerohedge

Reputation: 3765

Regex: Exclude first line

I've gone over solutions to similar answers on SE but they haven't worked for me.

Assume the following input:

@@A small note

Thanks

Here or [there][1]

A very nice thing

[1]: http://home.com

I want to exclude the first line. So my final output needs to be the text above, but starting with the line that says "Thanks" (not a blank line).

How can this be done?

Upvotes: 1

Views: 2181

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627409

You might consider matching the start of a line other than the first line in the string, that starts with a non-whitespace char, and then match everything up to the string end.

You may use

(?ms)(?!\A)^\S.*

See the regex demo.

Details:

  • (?ms) - MULTILINE (m) modifier making ^ match the start of a line and the DOTALL modifier (s) that makes a . match any chars including line breaks
  • (?!\A) - a negative lookahead that fails the match if the current location is the start of the string (\A always matches the start of a string regardless of the presence of the MULTILINE modifier)
  • ^ - start of a line
  • \S - a non-whitespace char (if there may be leading whitespace before the first non-whitespace char, add [^\S\r\n]* before \S)
  • .* - any 0+ chars up to the string end.

Upvotes: 1

Related Questions