Live data from Hacker News

Regex character "$" doesn't mean "end-of-string"

sethmlarson.dev

1–10 of 382 posts

Re: Regex character "$" doesn't mean "end-of-string"

#2
this is mostly due to the different types of regex and less about it being platform dependent. $ was end of string in pcre which is the "old" perl compatible regex. python has its own which has quirks as mentioned, re2 is another option in go for example, and i think rust has its own version as well iirc.

Re: Regex character "$" doesn't mean "end-of-string"

#3
post #2

this is mostly due to the different types of regex and less about it being platform dependent. $ was end of string in pcre which is the "old" perl compatible regex. python has its own which has quirks as mentioned, re2 is another option in go for example, and i think rust has its own version as well iirc.

Indeed, there isn't any kind of universal regexp standard.

Re: Regex character "$" doesn't mean "end-of-string"

#6
post #5

I'm confused by this blog-post. In the table: what is the reg-ex pattern tested and against which input?

The input being matched is "cat\n" and the regex pattern is one of:

  "cat$" with multiline enabled
  "cat$" with multiline disabled
  "cat\z"
  "cat\Z"

Re: Regex character "$" doesn't mean "end-of-string"

#7
Structural regexes as found in the sam editor are an obscure but well engineered regex engine. I am far from an expert but my main takeaway from them is that most regex engines have an implied structure built around "lines" of text. While you can work around this, it is awkward. Structural regexes allow you to explicitly define the structure of a match, that is, you get to tell the engine what a "line" is.

http://man.cat-v.org/plan_9/1/sam

Re: Regex character "$" doesn't mean "end-of-string"

#8
Regexp was one of the first things I truly internalized years ago when I was discovering Perl (which still lives in a cozy place in my heart due to a lovely “Camel” book).

Today most important bit of information is knowledge that implementations differ and I made a habit of pulling reference sheet for a thing I work with.

E.g. Emacs Regexp annoyingly doesn’t have word in form of “\w” but uses “\s_-“ (or something no reference sheet on screen) as character class (but Emacs has the best documentation and discoverability - a hill I’m willing to die on)

Some utilities require parenthesis escaping and some not. Sometimes this behavior is configurable and sometimes it’s not.

I lived through whole confusion, annoyance, denial phase and now I just accept it. Concept is the same everywhere but flavor changes.

Re: Regex character "$" doesn't mean "end-of-string"

#9
post #4

The new-line character is an actual character "at the end" of the string though so it makes sense that $ would include the new-line character in multi-line matching.

Yes and every implementation gets that right. The point was when multi-line matching is disabled and only Javascript, Go and Rust get that right.

I'm not too surprised by PHP and Python getting it wrong. Java and C# is a slight surprise though.

Post reply on HN