Regex to match only the first line?

Viewed 63262

Is it possible to make a regex match only the first line of a text? So if I have the text:

This is the first line.
This is the second line. ...

It would match "This is the first line.", whatever the first line is.

4 Answers

There is also negative lookbehind function (PowerGREP or Perl flavor). It works perfectly for my purposes. Regex:

(?<!\s+)^(.+)$

where

  • (?<!\s+) is negative lookbehind - regex matches only strings that are not preceded by a whitespace(s) (\s also stands for a line break)
  • ^ is start of a string
  • (.+) is a string
  • $ is end of string
Related