I'm looking for a regular expression that character #
and ##
only can occur once in a literal string.
It should match:
a#abc
a#bc##e
a##bc#e
a##e
But it should be non-compliant about:
a#a#b#c
a##bc##e
a##bc##e##d
a###e
I'm looking for a regular expression that character #
and ##
only can occur once in a literal string.
It should match:
a#abc
a#bc##e
a##bc#e
a##e
But it should be non-compliant about:
a#a#b#c
a##bc##e
a##bc##e##d
a###e
You could use the following regex, composed of an alternation of these two patterns:
##
is matched, then a single #
may appear
a single #
is matched, then ##
may appear
^[^#]*(?:##[^#]*#?|#[^#]*(?:##)?)[^#]*$
If the regex should match strings without any #
, just make the whole alternation optional :
^[^#]*(?:##[^#]*#?|#[^#]*(?:##)?)?[^#]*$