patternMinor
Regular Expression for the language that requires one symbol to occur at least once
Viewed 0 times
expressiononcethesymbolregularrequireslanguageonethatfor
Problem
I am trying to figure out the simplest way to do this using a regular expression.
More formally, $\{ w \in \{a,b,c\}^* ~|~ \#_a(w)\ge 1 \}$, where $\#_a(w)$ is the number
of $a$s in $w$.
The best I get is
$( ( b \mid c )^\, a\, ( b \mid c )^ )^+$
Is that the simplest way?
- Three symbols a, b, c.
- The sequence length is unlimited, i.e. *.
- The symbol a must be somewhere in the sequence at least once, but can appear more than once.
- The sequence may have only a.
More formally, $\{ w \in \{a,b,c\}^* ~|~ \#_a(w)\ge 1 \}$, where $\#_a(w)$ is the number
of $a$s in $w$.
The best I get is
$( ( b \mid c )^\, a\, ( b \mid c )^ )^+$
Is that the simplest way?
Solution
The simplest regular expression I can think of for this is $\left(a \mid b \mid c\right)^ a \left(a \mid b \mid c\right)^$. This is simpler than yours by the following measures of complexity:
- It contains less nesting (and fewer parentheses in general)
- It contains fewer quantifiers
Context
StackExchange Computer Science Q#1017, answer score: 7
Revisions (0)
No revisions yet.