• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Pattern Matching

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to validate product version entered by the user. The allowed pattern is any alphanumeric and a dot, i.e allowed values are:
1
1as.1dfd
11.11.hjh etc.

I am using the pattern as ^(\\w+.?\\w+)+$
The only problem with the ablove pattern is if the user gives only 1 digit or 1 letter, it fails to be matched

Can you please suggest a valid pattern.

Thanks, Please Help!
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The only problem with the ablove pattern is if the user gives only 1 digit or 1 letter, it fails to be matched



The problem is caused by the fact that you have two word patterns (of one or more characters) -- making all words between the periods to be at least two or more characters.

You may want to make the optional period attached to a zero length look-ahead... Instead of this...

.?\\w+

try....

(\\.(?=\\w))?



And BTW, the period has special meaning in a regex -- you will need to escape it... which is what I did above.

Henry
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't tested this, but you may want to try ^(?:\\w+\\.?)+$

John.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John de Michele wrote:I haven't tested this, but you may want to try ^(?:\\w+\\.?)+$

John.



That will also match a String like: "abc.def." (ie. a String ending with a DOT, which is not desired, if I understand the OP correctly).
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neh Agarwal wrote:I need to validate product version entered by the user. The allowed pattern is any alphanumeric and a dot, i.e allowed values are:
1
1as.1dfd
11.11.hjh etc.

I am using the pattern as ^(\\w+.?\\w+)+$
The only problem with the ablove pattern is if the user gives only 1 digit or 1 letter, it fails to be matched

Can you please suggest a valid pattern.

Thanks, Please Help!



Correct, that is because you used \\w+ twice, so, the minimum length of a String must be 2 to be matched.
Also note that the short-hand-character-class \w matches an underscore as well. If this is not what you want, you should use the following character class:



And the following regex will do the trick:

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet:

Oops! I think you're right. I forgot about the underscore and that pesky final dot.

John.
 
Neh Agarwal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, it works perfectly
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic