Skip to main content
added 87 characters in body
Source Link
Costas
  • 15k
  • 24
  • 38

Please be noted that expression try to choice the pattern of maximum length (gready regex) match. As you see in your example (regex: symbols between parenthesis) have choiced

  • ...is ( test.com) and (alex )

instead of

  • ...is (test.com) and (alex).

There are two ways to override such behavior:

  1. Substitute any symbol by revers match of limit or devide symbol (for example: (.*) by ([^)]*)
  2. Modern regular expressions (PCRE for example) allow a quantifier to be specified as lazy. They put a question mark after the quantifier to make it lazy .*?. By using a lazy quantifier, the expression tries the minimal match first.

More over the first variant allows to some simplify regex: grep -Po '\(\K[^)]*'

Please be noted that expression try to choice the pattern of maximum length (gready regex) match. As you see in your example (regex: symbols between parenthesis) have choiced

  • ...is ( test.com) and (alex )

instead of

  • ...is (test.com) and (alex).

There are two ways to override such behavior:

  1. Substitute any symbol by revers match of limit or devide symbol (for example: (.*) by ([^)]*)
  2. Modern regular expressions (PCRE for example) allow a quantifier to be specified as lazy. They put a question mark after the quantifier to make it lazy .*?. By using a lazy quantifier, the expression tries the minimal match first.

Please be noted that expression try to choice the pattern of maximum length (gready regex) match. As you see in your example (regex: symbols between parenthesis) have choiced

  • ...is ( test.com) and (alex )

instead of

  • ...is (test.com) and (alex).

There are two ways to override such behavior:

  1. Substitute any symbol by revers match of limit or devide symbol (for example: (.*) by ([^)]*)
  2. Modern regular expressions (PCRE for example) allow a quantifier to be specified as lazy. They put a question mark after the quantifier to make it lazy .*?. By using a lazy quantifier, the expression tries the minimal match first.

More over the first variant allows to some simplify regex: grep -Po '\(\K[^)]*'

Source Link
Costas
  • 15k
  • 24
  • 38

Please be noted that expression try to choice the pattern of maximum length (gready regex) match. As you see in your example (regex: symbols between parenthesis) have choiced

  • ...is ( test.com) and (alex )

instead of

  • ...is (test.com) and (alex).

There are two ways to override such behavior:

  1. Substitute any symbol by revers match of limit or devide symbol (for example: (.*) by ([^)]*)
  2. Modern regular expressions (PCRE for example) allow a quantifier to be specified as lazy. They put a question mark after the quantifier to make it lazy .*?. By using a lazy quantifier, the expression tries the minimal match first.