How to combine any appearance of a symbol, except when it occurs twice?

I try to match any occurrence of a plus sign at the end of the term, except when it occurs exactly two times. For example, the plus signs in c+and c++++must be matched, but not in c++.

The naive approach \+{1}$|\+{3,}$does not work, obviously. Do you have an idea?

+4
source share
3 answers

You can use regex with a negative view:

\b\+(?:\+{2,})?(?!\+)

RegEx Demo

RegEx Distribution:

  • \b - Pin Word Border
  • \+ - match literal +
  • (?:\+{2,})? - optionally matches 2 or more +
  • (?!\+) - + ++
+6

: regex builder , c +, ++, c 3 +

(c\+{1}|(\+){3,})
0

^[^+]*(?:\+|[+]{3,})$will match your far-fetched example, as well as anyone in the position cin your far-fetched example.

0
source

Source: https://habr.com/ru/post/1672587/


All Articles