• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex for alpha numeric ,apostrophes and hyphens with certian conditions

 
Ranch Hand
Posts: 37
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a requirement to validate a name field in java, where the conditions are like

"Only alpha and numeric characters, apostrophes and hyphens are acceptable. Spaces must
not appear immediately before or after apostrophes and hyphens".

For this I tried using many formats and finally I had something nearby




Could any one please help me to reovled the issue and provide the regex.

Nirmal
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an ambiguity in your requirement. You don't include a space in the set of allowable characters so by definition you can never have a space before or after apostrophes and hyphens!
 
author
Posts: 23951
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
This pattern ...

Nirmal Mukundan wrote:



is "one or more alpha or numeric characters, followed by a hyphen, followed by one or more of any character that is not a whitespace". Your requirement is...

Nirmal Mukundan wrote:
"Only alpha and numeric characters, apostrophes and hyphens are acceptable. Spaces must
not appear immediately before or after apostrophes and hyphens".

For this I tried using many formats and finally I had something nearby



To be blunt, what you have and what is your requirement aren't really close.

Perhaps the Oracle tutorial on regular expressions is a good place to start here (as regular expressions isn't something that should be done by trial and error) ... http://docs.oracle.com/javase/tutorial/essential/regex/

Henry
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Perhaps the Oracle tutorial on regular expressions is a good place to start here ... http://docs.oracle.com/javase/tutorial/essential/regex/



Assuming that the OP resolves the ambiguity by adding 'space' to the allowable set of characters then to use a single regex will likely require the use of 'zero-width negative lookahead' which is normally considered an advanced regex topic. I don't even know if it is covered by the tutorial but if it is it is certainly going to hit the OP in the solar plexus.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nirmal Mukundan wrote:"Only alpha and numeric characters, apostrophes and hyphens are acceptable. Spaces must
not appear immediately before or after apostrophes and hyphens".


My advice: Don't try to do it all in one regex.

Like Richard says, those "negative lookahead/lookbehind" searches are quite advanced stuff and difficult to follow.

What about checking that it contains only valid characters first, and then only checking for exceptions once you know that the basic characters are valid?
I suspect you'll find that a lot easier to write (and test); and it'll be a lot easier for other people to read too.

Winston
 
Nirmal Mukundan
Ranch Hand
Posts: 37
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for the responses. As Richard said, I should be checking the expression dividing it into 2.
I will try this out.

Regards
Nirmal
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nirmal Mukundan wrote: As Richard said, I should be checking the expression dividing it into 2.



I didn't say that! Winston did.

The required regex is not difficult if, within a single regex, one uses a cascade of two negative look ahead terms to check for the two exclusions and then a term to check for all characters being in the required set. All three terms are very simple so maintenance should not be a problem.

P.S. You still haven't resolved the ambiguity!
 
Nirmal Mukundan
Ranch Hand
Posts: 37
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

The expression can have spaces.

So as per the requirement, "Only alpha and numeric characters ,white space, apostrophes and hyphens are acceptable. Spaces must
not appear immediately before or after apostrophes and hyphens".

Kindly help me with the regex.

Regards
Nirmal
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nirmal Mukundan wrote:
Kindly help me with the regex.



I thought I had done by indicating what three terms are needed.
 
Henry Wong
author
Posts: 23951
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

Winston Gutkowski wrote:
My advice: Don't try to do it all in one regex.

Like Richard says, those "negative lookahead/lookbehind" searches are quite advanced stuff and difficult to follow.




I'll go one step further, if the OP is really new to regular expressions, and this is for a something that needs to be supported, then I recommend not using regular expressions at all. Regexes can get really complex, even for simple stuff, in the hands of a beginner -- and the last thing that you want is to create write-only code that can't be supported.

Henry
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote: and this is for a something that needs to be supported, then I recommend not using regular expressions at all. Regexes can get really complex, even for simple stuff, in the hands of a beginner -- and the last thing that you want is to create write-only code that can't be supported.



I'm quite uncomfortable with this advice. The OP's requirement cries out for a regular expression. Though not beginners stuff, the required regex is straightforward and will be maintainable. It consists of three simple terms that can be laid out in a manner that makes their function easy to understand. The OP will need to put in some effort to learn enough about regular expressions to complete this but the effort is unlikely to be wasted. Once one has the basics one finds regex very useful.




 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nirmal Mukundan wrote:

So as per the requirement, "Only alpha and numeric characters ,white space, apostrophes and hyphens are acceptable. Spaces must
not appear immediately before or after apostrophes and hyphens".

Kindly help me with the regex.



Try this regex.. You will get your requirement...!!


This will work..
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramesh Pramuditha Rathnayake wrote:

Nirmal Mukundan wrote:

So as per the requirement, "Only alpha and numeric characters ,white space, apostrophes and hyphens are acceptable. Spaces must
not appear immediately before or after apostrophes and hyphens".

Kindly help me with the regex.



Try this regex.. You will get your requirement...!!


This will work..




I really like this solution -- it almost works, and does so without using any look-aheads/look-behinds. I have to admit that I tend to abuse zero length look-aheads and look-behinds, so when I see a solution (that doesn't use any) for a use case that I think obviously needs it, I find the solution elegant. It does have a couple of minor flaws that can be easily fixed though -- it doesn't allow more than one whitespace in a row, and it doesn't allow the input to end in a whitespace, apostrophe, or hyphen (neither of these conditions were specified in the requirements).

Henry
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
I really like this solution -- it almost works, and does so without using any look-aheads/look-behinds. I have to admit that I tend to abuse zero length look-aheads and look-behinds, so when I see a solution (that doesn't use any) for a use case that I think obviously needs it, I find the solution elegant. It does have a couple of minor flaws that can be easily fixed though -- it doesn't allow more than one whitespace in a row, and it doesn't allow the input to end in a whitespace, apostrophe, or hyphen (neither of these conditions were specified in the requirements).
Henry



I did consider about not allowing white spaces at start and end. It was done by chance..!
But I intend not to use more whitespaces in a row in one time..
 
Henry Wong
author
Posts: 23951
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

Ramesh Pramuditha Rathnayake wrote:
I did consider about not allowing white spaces at start and end. It was done by chance..!
But I intend not to use more whitespaces in a row in one time..




Regardless, it is still elegant IMO*. Also, since it is not exactly what the OP wants, hopefully, the OP will take to the tutorials, and recommendations, instead of just cut-n-pasting this regex into code.

Henry






* BTW, you earned a cow !! ...

 
Nirmal Mukundan
Ranch Hand
Posts: 37
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnaks a lot for the solution, its working for our requirement.

Regards
Nirmal
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nirmal Mukundan wrote:Thnaks a lot for the solution, its working for our requirement.l



Interesting since it does not do what you asked for!
 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic