Core Spring Data
Written by: Oliver Gierke
Featured Refcardz: Top Refcardz:
  1. Git
  2. Prep for CD
  3. DNS
  4. Data Mining
  5. Spring Data
  1. Data Mining
  2. Spring Data
  3. DNS
  4. Machine Learning
  5. MongoDB

Link Details

Link 451743 thumbnail
User 530255 avatar

By mWo12
via noulakaz.net
Published: Jul 22 2010 / 08:02

Regular expressions are extremely powerful: /^1?$|^(11+?)\1+$/ checks if a number is prime or not.
  • 28
  • 1
  • 13074
  • 11

Comments

Add your comment
User 781709 avatar

mpo replied ago:

0 votes Vote down Vote up Reply

Uh, ok. Took some time to understand you first have to transform your number into a sequence of ones.

1 > 1 > match (no prime)
2 > 11 > no match (prime)
3 > 111 > no match (prime)
4 > 1111 > match (no prime)
etc etc

Allow me to question this technique as being practical for checking if let's say 25209506681 is a prime or not.

User 0 avatar

Scoundrel replied ago:

0 votes Vote down Vote up Reply

This isn't a regular expression, really. The \1 refers to the last match, and that requires remembering your matches -- and that's not strictly a regular expression... you need more than a Finite State Machine and it takes more than linear time to evaluate. (see Patterns for non-regular languages in http://en.wikipedia.org/wiki/Regular_expression).

For description of how this works, see http://montreal.pm.org/tech/neil_kandalgaonkar.shtml,

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.