Home > OS >  Regex: Symbol exactly once in string
Regex: Symbol exactly once in string

Time:10-22

I am struggling with a regular expression for detecting if a special character (hashtag #) is included exactly once in a string. Everything before and after the hashtag is unknown and can be any symbol (including whitespace and special characters).

Examples:

# matches

## doesnt match

a12bc#1def65 matches

a12bc##1def65 doesnt match

a234341#1212 matches

a23$. 4341#1 ._21&2 matches

#a23$. 4341#1 ._21&2# doesnt match

I think that would be quiet simple (I am not really good in regex) so I tried

#{1}

Surprisingly this does not work as I expected. This regex matches also the string ##. Then I tried some stuff like #{1}\b and (#{1}\b|[^#{2}]). But everytime either the string # dont match or ## do match the regex.

CodePudding user response:

This pattern #{1}\b can be written as #\b and matches # only if followed by a word boundary. This does not take a number of occurrences into account for a whole line.

This pattern (#{1}\b|[^#{2}]) has the same as before, followed by an alternation | and negated character class [^#{2}] that can match a single char other than chars # { 2 }


If you don't want to match newlines, and match the whole string:

^[^\r\n#]*#[^\r\n#]*$
  • ^ Start of string
  • [^\r\n#]* Optionally match any char except a newline or #
  • # Match the # char
  • [^\r\n#]* Optionally match any char except a newline or #
  • $ End of string

regex demo

  • Related