Ghostblade
Ghostblade

Reputation: 145

About the syntax of regular expression

I found a piece of code that accidentally outputs strange and inefficient regular expressions such as

a(a|a)b

which gets an a|a (two identical expressions connected by |)

Is such expression syntactically right? I can easily fix it, but I just want to know whether such expressions are valid or not, because this one seems bugs-free so far.

Upvotes: 0

Views: 67

Answers (2)

Rakesh KR
Rakesh KR

Reputation: 6527

What the regex a(a|a)b indicates is,

enter image description here

And the regex aab indicates

enter image description here

And both will matches the input aab

Upvotes: 1

Tom McClure
Tom McClure

Reputation: 6739

Yep, this is syntactically equivalent to the regex /a(a)b/ - just slower.

Upvotes: 4

Related Questions