简体   繁体   中英

How can I get my regular expression to return only the first match on the line?

My data contains lines like this:

55 511 00,"805, 809, 810, 839, 840",J223,201,338,116,16,200,115,6,P,S,"8,5","25,74",47,242,"55,7"

I have tried ,"(.*)", as a regular expression, but it captures too much of the line. This expression currently returns:

,"805, 809, 810, 839, 840",J223,201,338,116,16,200,115,6,P,S,"8,5","25,74",

but what I really want is just the first quoted string. Valid results would be:

  • ,"805, 809, 810, 839, 840",
  • 805, 809, 810, 839, 840

How can I capture only that first match?

You need to make the * lazy instead of greedy :

,"(.*?)",

or match all characters but " :

,"[^"]*",

Try "([^"]+) . Group 1 will match 805, 809, 810, 839, 840

/"([^"]+)"/

Will do the job! Everything between the "-s

Your regex is greedy, the .* will get everything up until the final "

So to make it non-greedy, add a ? at the end of the bracketed part:

,"(.*?)",

Which should stop it as soon as it reaches the next "

Use a Non-Greedy Match

There are many ways to handle this, but the simplest and most generic is to use a non-greedy match if your regular expression engine supports it. If it does not, you have to build an expression that knows a lot more about the structure of your data.

Some Examples

Here's an example using Perl-compatible regular expressions to split the output:

$ pcregrep -o '"(.*?)"' /tmp/foo | head -n1
"805, 809, 810, 839, 840"

Here's another example that uses pure Perl:

$ perl -ne 'print "$1\n" if /(".*?")/' /tmp/foo
"805, 809, 810, 839, 840"

Here's a third example that uses POSIX extended regular expressions, but which does not support non-greedy matches.

$ egrep -o '("[^"]+")' /tmp/foo | head -n1
"805, 809, 810, 839, 840"

You may also want to consider splitting your input into fields, and then testing each field until you find a match. A lot just depends on what facilities you have at your disposal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM