Regex with and without pipe

What’s the difference between [1][1|3|4] and [1][134]? I tried them both but they seem to have the same functionality.

[1|3|4] allows for | in the string, [134] does not.

So [1|3|4] would match 13|13|41 whereas [134] would not.

There are lots of online regex testers where you can play with this kind of stuff btw, like https://regex101.com/.

A great source for learning regex is https://www.regular-expressions.info/

1 Like

(not… quite?)

[1][1|3|4] whole-matches these four strings:
11
13
14
1|

Because neither of the character classes have a count modifier (*,+,?,{number[,[number]]}) on them, thus for the pattern to whole-match, there must be exactly 1 of each. [1][134] would only match the first three.

Partial Match (the “a match was found” test) would match any string with those things in it - "abscdo13||||||||||" would satisfy a searching match for either pattern, because it contains the pattern 13.

A whole-string match with a count modifier (^[134]+$) would fail to match 13|13|41 whereas ^[1|3|4]+$ would, which i think is what you were trying for?

2 Likes

I was, I missed that there were no quantifiers, so yes, everything you said is true and what I said was not :slight_smile:

1 Like

It’s also worth pointing out that this entire discussion is completely dependant on the pipe in question being inside a character class - outside of that, it would have entirely different meaning/effect.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.