Is there any way to see the string that was matched in grep?
8
votes
1
answer
524
views
**I'm not talking about -o option.**
[Posix](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03) says:
> The search for a matching sequence starts at the beginning of a string and stops when the **first sequence matching the expression is found**, where "first" is defined to mean "begins earliest in the string". If the pattern permits a variable number of matching characters and thus there is more than one such sequence starting at that point, the **longest such sequence is matched**. For example, the BRE "bb*" matches the second to fourth characters of the string "abbbc", and the ERE "(wee|week)(knights|night)" matches all ten characters of the string "weeknights".
And I want to **verify** what is being said in posix and this tutorial [regTutorialSite](https://www.regular-expressions.info/posix.html) :
> A POSIX-compliant engine will still find the **leftmost match**. If you **apply** Set|SetValue to Set or SetValue **once**, it **will match Set**.
How to "apply once"?
When i run **grep -o** the result is two strings: Set and SetValue, but not just " one leftmost " . That is, I read about one thing, but in practice I get something else. So, how to see what string was matched by regex?
(Perhaps the question was formulated incorrectly or could have been better)
Asked by Mark
(99 rep)
Aug 1, 2025, 12:59 PM
Last activity: Aug 2, 2025, 07:19 AM
Last activity: Aug 2, 2025, 07:19 AM