Hacker News new | ask | show | jobs
by Sharlin 1010 days ago
As ^ and $ are implicit, you can opt out of them simply by affixing `.*`.
1 comments

Only when the ^ or $ were at the start/end of your string is it simple. Eg:

    (a|b|^)(c|d|^)foo
Rewriting without ^ can require much longer regex.
Isn't that just

    ((a|b)?(c|d)|c|d)?foo
Unless you mean it as a search expression, in which case it's more like

    ((.*a|.*b)(c|d)|c|d)?foo
Which I have to admit was a lot harder to figure out than I thought it would be (and may not even be right!)
Yeah the latter.

In an engine supporting ^ and $, searching for this

    (a|b|^)(c|d|^)foo
is equivalent to searching for this

    ^((.*a|.*b)(c|d)|c|d)?foo.*$
And in this context you can drop the leading/trailing ^/$ since they are implicit.