News for hackers. Stuff that matters.
Re:A regular expression to check for prime numbers by wongarsu on Tuesday June 20, 2023 @10:14PM (#36413544)
When programmers say regular expression they usually mean Perl Compatible Regular Expressions (PCRE). Or I guess PCRE-compatible expressions, since there are other implementations of the same syntax out there. If you go back far enough in time they were once regular expressions in the computer science sense, but then features got added, and now they are incredibly powerful but can take basically infinite memory and time.

https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expres...

Re:A regular expression to check for prime numbers by disconcision on Tuesday June 20, 2023 @10:24PM (#36413615)
funnily enough the original POSIX regular expressions (BRE) were strictly less powerful than regular expressions (CS sense), lacking a general alternation operator (though one was provided in ERE)
Re:A regular expression to check for prime numbers by chrismorgan on Tuesday June 20, 2023 @11:27PM (#36414032)
Although general PCRE syntax has clearly won, that’s distinct from the infinite-memory-and-time matter. Engines that don’t use backtracking and can search in linear time (O(regex-size × search-text-size)) have been making a serious comeback in recent years, at only the cost of not supporting lookaround assertions or backreferences. For example, the re2 library, or Rust’s regex crate, both of which are very popular.
Re:A regular expression to check for prime numbers by knome on Wednesday June 21, 2023 @12:13AM (#36414318)
Russ Cox has an excellent write up on it.

"Regular Expression Matching Can Be Simple And Fast" (2007)

https://swtch.com/~rsc/regexp/regexp1.html

Re:A regular expression to check for prime numbers by eru on Wednesday June 21, 2023 @07:36AM (#36417183)
Alas that approach only works well for your basic regular expressions that support Kleene-Star, concatenation and union.

Regular languages are also closed under intersection and taking prefix etc (see https://en.wikipedia.org/wiki/Regular_language#Closure_prope...), and if you want to expose those operators in your regular expressions, Russ Cox's technique doesn't really work.

Re:A regular expression to check for prime numbers by knome on Wednesday June 21, 2023 @09:55AM (#36418502)
I suspect it should be possible to transform two intersected (K*,C,U) expressions into a new (K*,C,U) expression by walking over the two, dropping incompatible terms, reducing sets, and redistributing and expanding sub-expressions to combine the two, though I do not have time to take a serious look at this at the moment.

I also suspect that this might result in a combinatorial explosion of fused terms, but that's irrelevant as we're only worried about whether intersection can be implemented in Russ Cox's method, not whether a regular expression might require several earths or so worth of RAM to hold :)

Nothing in the wiki article mentions taking a prefix, but that just sounds like `(prefix|)`?

Re:A regular expression to check for prime numbers by eru on Wednesday June 21, 2023 @07:47PM (#36426355)
You can still explicitly build the finite automaton for your closure-extended regular language. That one might be big, but matching is still linear in the length of the text you are searching through.

As you point out Russ Cox's method can not deal with complement and intersection. You can hack it up by putting a compiler in front, but then you might as well compile to finite automata directly. Or you use derivatives: https://en.wikipedia.org/wiki/Brzozowski_derivative or https://www.ccs.neu.edu/home/turon/re-deriv.pdf (or https://well-typed.com/blog/2020/06/fix-ing-regular-expressi... for a Haskell flavour).

> Nothing in the wiki article mentions taking a prefix, but that just sounds like `(prefix|)`?

They implicitly mention prefixes, when they talk about 'the trio operations: string homomorphism, inverse string homomorphism, and intersection with regular languages.' If I remember right, the prefix operation is:

Take a language L, restrict it to the strings that have the right prefix: `L' := L & (prefix.*)` then chop off that prefix from every member of L'.

See https://en.wikipedia.org/wiki/Quotient_of_a_formal_language which this is a special case of. The Brzozowski derivative article also explains it in more detail.

Re:A regular expression to check for prime numbers by finitemonkey on Wednesday June 21, 2023 @01:36AM (#36414763)
> and now they are incredibly powerful but can take basically infinite memory and time.

Which is why the "RE" in the article is excrutiatingly slow, given that it needs to perform insane amounts of backtracking. In contrast, "real" regular expression checkers run in linear time.

Also, nitpicking, but they take unbounded time. It's still finite.

Re:A regular expression to check for prime numbers by petters on Wednesday June 21, 2023 @02:21AM (#36415059)
They said “basically,” so no need to nitpick. And I’m not sure about unbounded; surely it is bounded by a suitable exponential function?
Re:A regular expression to check for prime numbers by cubefox on Wednesday June 21, 2023 @07:05AM (#36416960)
I think with "unbounded but finite" he meant potential infinity rather than actual infinity.
Re:A regular expression to check for prime numbers by finitemonkey on Wednesday June 21, 2023 @05:49PM (#36425133)
> They said “basically,” so no need to nitpick.

The inflationary use of "infinite" especially in tech crowds that should know better deserves pushback.

There are models of computation that actually model infinite computation. A well-studied one is Büchi automata, basically a generalization of finite state automata to infinite sequences.

> And I’m not sure about unbounded; surely it is bounded by a suitable exponential function?

Nope. You can nest them and for any function that you give you can produce a RE that takes longer than that function to evaluate. (That's literally what "unbounded" means. There is no bound. Doesn't mean it's "infinite". All those computations terminate, which is literally what "finite" means in this context.)

Re:A regular expression to check for prime numbers by tannhaeuser on Wednesday June 21, 2023 @05:27AM (#36416319)
> When programmers say regular expression they usually mean Perl Compatible Regular Expressions (PCRE).

Nope.

Re:A regular expression to check for prime numbers by elros on Wednesday June 21, 2023 @07:52AM (#36417294)
So what is it that they mean, in your opinion?
Re:A regular expression to check for prime numbers by tannhaeuser on Wednesday June 21, 2023 @08:23AM (#36417557)
Re:A regular expression to check for prime numbers by jkrems on Wednesday June 21, 2023 @09:30AM (#36418206)
And that article says:

> Perl regexes have become a de facto standard, having a rich and powerful set of atomic expressions.