BIRT 3.7
Written by: Michael Williams
Featured Refcardz: Top Refcardz:
  1. Eclipse for Spring
  2. MVVM Pattern
  3. Eclipse RCP
  4. Silverlight/WP7
  5. Eclipse Plug-ins
  1. BIRT 3.7
  2. Ajax
  3. Eclipse
  4. jQuery Selectors
  5. Spring Config.

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.
  • 30
  • 1
  • 11559
  • 5

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.