Parsing only first regex match in a line with several matches

前端 未结 2 423
野性不改
野性不改 2021-01-21 15:42

Is it possible to have a regex that parses only a1bcdea1 from this line a1bcdea1ABCa1DEFa1 ?

This grep command does not work:

         


        
2条回答
  •  不思量自难忘°
    2021-01-21 16:14

    How about using a ^ start anchor and restricting character set used:

    grep -o '^[A-Za-z]1[A-Za-z]*1'
    

    See this Bash demo or Regex Pattern at regex101

    If you expect more digits or other characters in between, go with this

    grep -oP '^[A-Za-z]1.*?[A-Za-z]1'
    

    The lazy matching requires perl compatible mode. For not at line start, go with this

    grep -oP '^.*?\K[A-Za-z]1.*?[A-Za-z]1'
    

    \K resets beginning of the reported match and is a PCRE feature as well.

提交回复
热议问题