Regular expression to stop at first match

publicRavi

My regex pattern looks something like

<xxxx location="file path/level1/level2" xxxx some="xxx">

I am only interested in the part in quotes assigned to location. Shouldn't it be as easy as below without the greedy switch?

/.*location="(.*)".*/

Does not seem to work.

Daniel Vandersluis

You need to make your regular expression non-greedy, because by default, "(.*)" will match all of "file path/level1/level2" xxx some="xxx".

Instead you can make your dot-star non-greedy, which will make it match as few characters as possible:

/location="(.*?)"/

Adding a ? on a quantifier (?, * or +) makes it non-greedy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Grep regular expression stop after first match

From Dev

Regular expression stop after first match

From Dev

Regular expression to stop at first found subpattern

From Dev

Regular expression to stop at first found subpattern

From Dev

Regular Expression (Replace) only first match

From Dev

Regular expression to match first and last character

From Dev

Regular expression to find first match only

From Dev

Is regular expression search guaranteed to return first match?

From Dev

Regular expression match all except first occurence

From Dev

Regular expression to capture until the first match

From Dev

Is regular expression search guaranteed to return first match?

From Dev

Regular expression to match first and last character

From Dev

Regular expression to match the first appearance of a character

From Dev

gcloud stop exception - regex / Values must match the following regular expression

From Dev

Regular expression - find from start to end but only first match

From Dev

R : regular expression to match pattern in only the first line

From Dev

Python Regular Expression - Match certain first characters but not a W

From Dev

Qt Regular Expression selects from first of first match to end of last match

From Dev

MSVC regular expression match

From Dev

Regular Expression Match - Javascript

From Dev

ruby match regular expression

From Java

Regular expression selectively match

From Dev

Regular expression to match | but not ||

From Dev

Regular Expression Following Match

From Dev

SPARQL regular expression not match

From Dev

Fuzzy match by regular expression

From Dev

Match Regular Expression in Javascript

From Dev

Regular expression to match year?

From Dev

Regular expression for match

Related Related

  1. 1

    Grep regular expression stop after first match

  2. 2

    Regular expression stop after first match

  3. 3

    Regular expression to stop at first found subpattern

  4. 4

    Regular expression to stop at first found subpattern

  5. 5

    Regular Expression (Replace) only first match

  6. 6

    Regular expression to match first and last character

  7. 7

    Regular expression to find first match only

  8. 8

    Is regular expression search guaranteed to return first match?

  9. 9

    Regular expression match all except first occurence

  10. 10

    Regular expression to capture until the first match

  11. 11

    Is regular expression search guaranteed to return first match?

  12. 12

    Regular expression to match first and last character

  13. 13

    Regular expression to match the first appearance of a character

  14. 14

    gcloud stop exception - regex / Values must match the following regular expression

  15. 15

    Regular expression - find from start to end but only first match

  16. 16

    R : regular expression to match pattern in only the first line

  17. 17

    Python Regular Expression - Match certain first characters but not a W

  18. 18

    Qt Regular Expression selects from first of first match to end of last match

  19. 19

    MSVC regular expression match

  20. 20

    Regular Expression Match - Javascript

  21. 21

    ruby match regular expression

  22. 22

    Regular expression selectively match

  23. 23

    Regular expression to match | but not ||

  24. 24

    Regular Expression Following Match

  25. 25

    SPARQL regular expression not match

  26. 26

    Fuzzy match by regular expression

  27. 27

    Match Regular Expression in Javascript

  28. 28

    Regular expression to match year?

  29. 29

    Regular expression for match

HotTag

Archive