Pirates
Pirates

Reputation: 13

What is the difference between these two regular expression statements

([1|2|3|4|5|6|7|8|9]) and (1|2|3|4|5|6|7|8|9)

I would like to check each port of a Cisco Switch for a specified set of commands. The ports are numbered 1 thru 9. Using a regular expression I want the code to choose each one of the numbers to validate the configuration.

Upvotes: 1

Views: 99

Answers (4)

Cris Mooney
Cris Mooney

Reputation: 304

As others noted, "|" in the first is obviously errant, since there would be no need to repeat it, and thus should be [123456789] or [1-9]. Beyond that, it is also worth noting that in a numeric context there would be no functional difference with this exact data, hiding a logical bug, because they both happen to contain only single characters (the second "strings" just happen to be single characters). However, ([1|2|3|4|5|6|7|8|9|10]) and (1|2|3|4|5|6|7|8|9|10) would not be functionally the same even in the numeric context, since only the first would match "0" (with redundant "1" as well as extraneous "|") and only the second would match 10. Noting this might further help clarify confusion, and potential bugs that make this important.

Upvotes: 1

Arcturus B
Arcturus B

Reputation: 5621

The first one will match any of:

1 2 3 4 5 6 7 8 9 |

while the second one will match any of:

1 2 3 4 5 6 7 8 9

Note that the first one is equivalent to: ([123456789|]).

Of course, the regex that would do the job for your problem is (slightly) different: ([123456789]), or ([1-9]).

Upvotes: 1

erip
erip

Reputation: 16935

The first regular expression doesn't really make sense: it means "choose 1 or choose | or choose 2 or choose 3 or ... or choose 9"

The second means "choose 1 or choose 2 or choose 3 or ... or choose 9"

I think the regular expression you were looking for in the first regular expression is ([123456789]), which is exactly equivalent to the second regular expression.

To make it even simpler, you can use ([1-9]), which just indicates a range.

For more information about regular expressions, more specifically perl compatible regular expressions (PCREs), you can look here.

Upvotes: 6

pquest
pquest

Reputation: 3290

The first one will match any character in the set

|123456789

The second one will match

1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9

.

Upvotes: 1

Related Questions