Regular Expression to match only one angle bracket

后端 未结 3 917
轻奢々
轻奢々 2020-12-21 22:47

I\'m looking for a regular expression that matches the \'>\' in

a > b
>
b>
...

but not two or more angled brackets, i.e. it should

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-21 23:16

    perreal's first regex is correct. However, the second regex given in that answer subtly fails in one condition. Since it captures characters both before and after, two >s separated by a single character will not both be found.

    Here is a regex which only uses forward lookaheads and doesn't have that problem:

    (?:^|[^>])(>)(?:$|(?!>))
    

    Regular expression image

    Edit live on Debuggex

提交回复
热议问题