• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Regular expression format

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there anyway to write a regular expression to check the number fall  in between range 1402 to 1423?

What is the regular expression format for the numbers?

Var s= (1402,1403,1404…. 1423)

Thanks
 
Sheriff
Posts: 67693
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not what a regular expression is intended for. What's wrong with good old comparisons?
 
Master Rancher
Posts: 4353
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's certainly possible - but probably not a good idea, unless as a puzzle or practice for writing regular expressions.  Or in rare cases where somehow the regex is the only option you have, and you can't just write and execute your own Java / JavaScript / whatever to validate more effectively (and readably).

If you still really want or need to do this anyway... what do you have so far?  What parts of the pattern will be there in all cases, and what parts are only there for certain situations?  At some point you will probably want to join several alternative patterns together using '|'.  What can you come up with?
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you use a hammer to drive a screw into a board?  Probably, but it's not the best tool for the job.

That's what you're asking to do with a regex here.  
 
Saloon Keeper
Posts: 26536
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anvi kon wrote:
What is the regular expression format for the numbers?

Var s= (1402,1403,1404…. 1423)



14\d\d

But as others have said, regex doesn't do range checks.. That regex can only tell numbers between 1400 and 1499. You'd have to convert to an actual number to do the range check.

So you can use a regex to determine / extract a number from a string, but the value of the number requires more than just regex.
 
Mike Simmons
Master Rancher
Posts: 4353
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, again, you can do it:

But, is it readable?  And if you want to change the range later, how likely is it that someone will make an error when trying to change it?  Chances are, a non-regex (or partial-regex) solution will be better for you.
 
fred rosenberger
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My regex skills are rusty, but wouldn't something like this work:

14(02|03|04|05...|23)
 
Mike Simmons
Master Rancher
Posts: 4353
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it would.  My regex was a variation on that.
 
fred rosenberger
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Yes, it would.  My regex was a variation on that.


but mine is easier to read!!!  ;-)
 
Mike Simmons
Master Rancher
Posts: 4353
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure?  You felt the need to abbreviate it.

Of course, if we expand the range, i think my version will scale better.
 
Tim Holloway
Saloon Keeper
Posts: 26536
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about /19[0[3-9]|1\d|2[0-3]]/?

Or did I botch the syntax?
 
Mike Simmons
Master Rancher
Posts: 4353
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That syntax is a little off.  Mostly, your outermost pair of braces [ ] need to be parentheses ( ).  Braces represent a character class, and there's no way to nest a character class within another character class.  But once you replace those outermost braces, it's basically equivalent to the regex I gave.  I like using (?: ) to create a non-capturing group, where the parens are only there for grouping precedence, not to tell the regex to save a value in memory for later re-use.  I have the idea this should be more efficient, even though it's probably a negligible effect, and it reduces readability a little.  And you can also use \d instead of [0-9], sure.  
 
Tim Holloway
Saloon Keeper
Posts: 26536
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Parentheses are not the same as brackets. Parentheses in regexps (regexes, whatever) are grouping operators. And yes, I'm pretty sure that not only can you do brackets inside of brackets, but also that I've done so more than once. Regexps are generally handled by compiling the pattern and running it through a finite-state machine and stuff like that works fine in such an environment.

As for the "?:" notation, I tend to use it sparingly, since I work with regexes in Java, Perl, awk, sed and Python and not all of them have as rich a kit. Plus "?" to me mentally means some sort of optional, and the only option here is which sub-pattern you'll match.
 
Mike Simmons
Master Rancher
Posts: 4353
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We're in agreement that braces and parentheses are not the same thing in regexes.  However, we don't agree on what they mean when nested.  After checking documentation, I see that something that looks like nesting is possible.  Though it doesn't really seem to apply here.  Moreover, when I take your code and try to test it, I can't get the expected results - unless I replace the outer braces with parens, as I said earlier.  We also need to replace 19 with 14, but that's a minor typo.  I also doubled the \ to \\, but that's just part of translating to Java, unfortunately.

I believe your regex is representing a "19" followed by one big character class, representing a single character, which can be basically any digit, or \.  I'm not sure what happens to the \\d within a character class though.
 
Tim Holloway
Saloon Keeper
Posts: 26536
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Meh, 14, 19, whatever. No, I knew I couldn't do that without missing at least one obvious thing. Never can.

But yes, brackets match on ONE of the characters in the bracket, not a substring, so even if [1[3-9]] is legal, it should be presumably equivalent to [13-9]. I must have mis-remembered something. -1 point.

Because "()" forces grouping, I'm not sure it's the definitive way to deal with "or-bar" matching. But I'd have to read the manual to be sure and I'd rather slink away than do that much work.

Anyway, I think that we'd all agree that sane people should just match "(14\\d\\d)", extract the match value, convert it to an integer and do an arithmetic comparison.
 
fred rosenberger
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Are you sure?  You felt the need to abbreviate it.


That's because I'm lazy and was just illustrating a point.

The OP asked for "any way". And as we've seen, there are "many" ways.  None are particularly GOOD, however.
 
Mike Simmons
Master Rancher
Posts: 4353
59
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My "are you sure?" was intended as a response to "mine is easier to read".  We agree on all other points.

Incidentally I omitted one value, 18, from the "expanded form" of your regex.  Point being, it's easy to overlook a bug in something like that.  Then again, it would have also been easy to insert a bug in my own regex, that might be similarly hard to notice.  Ah well.
 
reply
    Bookmark Topic Watch Topic
  • New Topic