Regular Expression to match only one angle bracket

后端 未结 3 847
轻奢々
轻奢々 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

    0 讨论(0)
  • 2020-12-21 23:23

    Perl syntax:

    /(?<!>)>(?!>)/
    

    Without using lookahead or lookbehind:

    /(?:^|[^>])>(?:[^>]|$)/
    
    0 讨论(0)
  • 2020-12-21 23:24

    The issue here is that when you use the lookahead, you're matching the second > (there's no > after the second >), and when you're using the lookbehind, you're matching the first >.

    You could probably use this:

    [^>]>[^>]
    

    There's a >, with no > before or after it.

    But I think that to suit what you exactly need, need to use lookaheads and lookbehinds both:

    (?<!>)>(?!>)
    
    0 讨论(0)
提交回复
热议问题