Question

In the chapter 1 of Introducing regular expression, I see a regex like this:

^(\(\d{3}\)|^\d{3}[.-]?)?\d{3}[.-]?\d{4}$

enter image description here

I'm a little confused with it because the second ^ looks redundant to me. The | separates two alternatives \(\d{3}\) or ^\d{3}[.-]? within the parenthesis, and there is aleady a ^ outside the parenthesis, which I understand as to match the start of a line, so I think the second ^ in ^\d{3}[.-]? is not necessary to match the line begining. Does anyone have ideas about this?

Était-ce utile?

La solution

It does look redundant, but there is one possible explanation for it that would be valid (albeit non-sensical given the context).

You've only included the regex pattern in the question; what you haven't shown us is whether any modifiers are being used.

If you use the m modifier to switch the regex parser into multi-line mode, then the ^ and $ anchors change their meaning so that they match the start and end of a line, as well as of the entire string.

Therefore, if your expression was using the m modifier, the additional ^ would tell it to look for an extra line-feed in that particular instance. So it would have an effect on the expression.

But ultimately, looking at what the expression you quoted actually does, I doubt that this is what is intended; it does look as if it's basically a mistake, as you assume.

Autres conseils

Yes, it looks redundant to me as well. The first anchor is sufficient.

Here's how I believe that breaks down into parts:

^
(
    \(\d{3}\)
    |
    ^\d{3}[.-]?
)?
\d{3}
[.-]?
\d{4}
$

Yes, it's redundant and useless there. well, it doesn't crash ;)

^(\(\d{3}\)|^\d{3}[.-]?)?\d{3}[.-]?\d{4}$

Regular expression visualization

Debuggex Demo

The ^ marks the string/line start and doesn't create any result and also the pointer internally isn't moved, so the expressions ^, ^^ or ^^^^^^^ are all equal.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top