Docsity

Regular Expression Examples: Identifying Patterns in Strings using RegEx, Exams of Computer Science

Toggle sidebar
bg1
Regular Expression Examples
(Realize that many different RegExs can describe the same regular language.)
1. (0
1) = { w | w is exactly 0 or 1} (literally only two strings are allowed: 0 and 1.)
2. 0*10* = { w | w has exactly a single 1}
3. ∑*1∑* = {w | w has at least one 1}
4. ∑*101∑* = {w | w has the substring 101}
5. (∑∑)* = {w | w has an even length}
6. ∑(∑∑)* = {w | w has odd length}
7. (∑∑∑)* = {w | the length of w is a multiple of 3}
8. language of all strings ending with 0 = ∑*0
9. language of all strings not ending with 11
(So if the length is less than 2 we have no restriction, I choose to enumerate the possibilities.)
= ε
0
1
∑*00
∑*01
∑*10 (Enumerating all possibilities)
= ε
∑
∑*00
∑*01
∑*10 (realized I could simplify)
= ε
∑
∑*(00
01
10) (simplified even more)
10. language of all strings w such that w contains exactly one 1 and an even number of 0s.
Let's jot a few down: 1, 001, 010, 100, 00001, 00010, 00100, 01000, 10000, and so on
Now for the regular expression:
= (00)*1(00)*
(00)*010(00)*
(Think about it, for the count of 1s to be even, then viewing the 1 as splitting those 0s, we have
then an even number of 0s both before and after OR we have an odd number of 1s both before
and after (to maintain the total overall even count of 0s.))
11. Set of strings over {0, 1, 2} containing at least one 0 and at least one 1.
(Again the problem is to identify categories of possibilities. We need a 0; we need a 1; Anything
can be dispersed before and after those. We also need to make sure that at a minimum even if
we have exactly one 0 and one 1, the order could be 01 or 10. Hence the two regular expressions
below unioned together into one regular expression.
= ∑*0∑*1∑*
∑*1∑*0∑*
12. Back to our typical alphabet, {0,1}, language of all strings containing at most one pair of
consecutive 0s.
I start by thinking about the minimal essence of what is required: the string may have no
sequences of 0s or just a single 0, or exactly two 0s. I write the regular expression for just that
part – ignoring the context in which this will be allowed.
(ε
0
00) just the kernel of the problem
Now I reason that 0 and 1 are allowed before and after this pattern, but with some restrictions so
that I do not allow inadvertently a sequence of more than two 0s. Before this pattern, certainly
any number of strictly 1s is allowed (that's 1*), but intermixed I could have a 0 as long as it is
guaranteed to be followed by a 1 so that concatenating that with our kernel part will not result in
a series of more than two 0s in sequence. Hence I have:
(1
01)* (ε
0
00).
Using the same sort of reasoning I describe what is allowed after the kernel. And here is the
final RegEx:
(1
01)* (ε
0
00) (1
10)* (Admittedly this may not be the most elegant sol'n.)
pf3

Partial preview of the text

Download Regular Expression Examples: Identifying Patterns in Strings using RegEx and more Exams Computer Science in PDF only on Docsity!

Regular Expression Examples

(Realize that many different RegExs can describe the same regular language.)

  1. = { w | w is exactly 0 or 1} (literally only two strings are allowed: 0 and 1.)
  1. 010 = { w | w has exactly a single 1}
  2. ∑1∑ = { w | w has at least one 1}
  3. ∑101∑ = { w | w has the substring 101}
  4. (∑∑)* = { w | w has an even length}
  5. ∑(∑∑)* = { w | w has odd length}
  6. (∑∑∑)* = { w | the length of w is a multiple of 3}
  7. language of all strings ending with 0 = ∑*
  8. language of all strings not ending with 11

(So if the length is less than 2 we have no restriction, I choose to enumerate the possibilities.)

= ε

 0

 1

 ∑*

 ∑*

 ∑*10 (Enumerating all possibilities)

= ε

∑

∑*

∑*

∑*10 (realized I could simplify)

= ε

 ∑

 ∑*(

  1. (simplified even more)
  1. language of all strings w such that w contains exactly one 1 and an even number of 0s.

Let's jot a few down: 1, 001, 010, 100, 00001, 00010, 00100, 01000, 10000, and so on

Now for the regular expression:

 (00)010(00)

(Think about it, for the count of 1s to be even, then viewing the 1 as splitting those 0s, we have

then an even number of 0s both before and after OR we have an odd number of 1s both before

and after (to maintain the total overall even count of 0s.))

  1. Set of strings over {0, 1, 2} containing at least one 0 and at least one 1.

(Again the problem is to identify categories of possibilities. We need a 0; we need a 1; Anything

can be dispersed before and after those. We also need to make sure that at a minimum even if

we have exactly one 0 and one 1, the order could be 01 or 10. Hence the two regular expressions

below unioned together into one regular expression.

 ∑1∑0∑*

  1. Back to our typical alphabet, {0,1}, language of all strings containing at most one pair of

consecutive 0s.

I start by thinking about the minimal essence of what is required: the string may have no

sequences of 0s or just a single 0, or exactly two 0s. I write the regular expression for just that

part – ignoring the context in which this will be allowed.

(ε

 0

  1.  just the kernel of the problem

Now I reason that 0 and 1 are allowed before and after this pattern, but with some restrictions so

that I do not allow inadvertently a sequence of more than two 0s. Before this pattern, certainly

any number of strictly 1s is allowed (that's 1*), but intermixed I could have a 0 as long as it is

guaranteed to be followed by a 1 so that concatenating that with our kernel part will not result in

a series of more than two 0s in sequence. Hence I have:

01)* (ε

0

00).

Using the same sort of reasoning I describe what is allowed after the kernel. And here is the

final RegEx:

 01)* (ε

 0

  1. (

 10)* (Admittedly this may not be the most elegant sol'n.)

  1. language of all strings w such that w contains at most one 1 OR at least two 0s. (Implied: or

both.)

a) let's deal with the "at most one 1"  010

0*  0*(ε

1)0*

b) now the "at least two 0s"  ∑0∑0∑*

so since we are describe the language of all strings that belong in a or b, we use union:

0*(ε

 1)0*

 ∑0∑0∑*

  1. Compare 12 to this problem. The language of all strings w | w contains at most one 1 AND at

least two 0s.

Trying to use info of a & b from above will give a solution, albeit a convoluted line of reasoning:

i)

It is useful to find a & b as in problem 12, but in the end we seek the intersection of those regular

expressions a & b. Not the union.

Focusing on b) ∑0∑0∑* we observe that at most only one of the three ∑* is allowed to 010

while the other two are allowed to be 0*; otherwise we end up with too many 1s being allowed.

(And all 0s is okay.)

all 0s  000*

allowing the first ∑* yields (010)000  01000

allowing the second ∑* yields 00(010)00  0010

allowing the third ∑* yields 0000(010)  00010

Putting those three together we have now:

 001

 010

 100)0*

ii)

Instead it may have been more productive just to think of this with a fresh mind. If at most one 1

is allowed and at least two 0s, then I can start enumerating the strings of this language taken in

order of length:

(None of zero length; none of length one)

and I realize others are the result of just interjecting more 0s within those string I have listed. So

I have the kernel of the RegEx.

 001

 010

and since that may have any number of 0s before or after it:

 001

 010

 100)0*