Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match only the first line?

Tags:

regex

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.

like image 816
Juan Avatar asked Sep 03 '25 05:09

Juan


2 Answers

that's sounds more like a job for the filehandle buffer.

You should be able to match the first line with:

/^(.*)$/m

(as always, this is PCRE syntax)

the /m modifier makes ^ and $ match embedded newlines. Since there's no /g modifier, it will just process the first occurrence, which is the first line, and then stop.

If you're using a shell, use:

head -n1 file

or as a filter:

commandmakingoutput | head -n1

Please clarify your question, in case this is not wat you're looking for.

like image 188
polemon Avatar answered Sep 04 '25 23:09

polemon


In case you need the very first line no matter what, here you go:

 \A.*

It will select the first line, no matter what.

like image 36
Ivaprag Avatar answered Sep 04 '25 23:09

Ivaprag