• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

regular expression help

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have following requirement

alphanumeric with minimum 4 and maximum 32 ( should contain atleast 1 number )

help me.........................
 
author & internet detective
Posts: 42103
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My first piece of advice is to not do the whole thing in a regular expression. You can write a simple regular expression for 4-32 alphanumeric characters. Then just add an if statement to check the string contains a number.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:My first piece of advice is to not do the whole thing in a regular expression. You can write a simple regular expression for 4-32 alphanumeric characters. Then just add an if statement to check the string contains a number.



I have to disagree with this. It is very easy to check for 4-32 alphanumerics and very easy to use a positive lookahead to look for a decimal digit. I would post the solution but won't until there is something that shows the OP has put some effort into this.
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
below is my regular expression

[a-zA-z_0-9]{4,32}


i can easily add condition for minimum single digit at the start or at the end of the regular expression but couldn't find any info online for adding anywhere in the expression

any reference to website is also ok, i can figure it out

 
Jeanne Boyarsky
author & internet detective
Posts: 42103
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Sabre wrote:I have to disagree with this. It is very easy to check for 4-32 alphanumerics and very easy to use a positive lookahead to look for a decimal digit. I would post the solution but won't until there is something that shows the OP has put some effort into this.


That's true. I was thinking that the problem was hard for arbitrary counts. For one, you are correct that lookahead is just fine. Which is the actual problem here.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen gupta wrote:below is my regular expression

[a-zA-z_0-9]{4,32}



Hopefully the 'A-z' part of the above is just a typo.


i can easily add condition for minimum single digit at the start or at the end of the regular expression but couldn't find any info online for adding anywhere in the expression

any reference to website is also ok, i can figure it out



You need to take the hint and look at "positive lookahead". The syntax is given in the Javadoc for Pattern and a good starting point for understanding "positive lookahead" is http://www.regular-expressions.info/tutorial.html . What you are looking ahead for is that somewhere in the target string there is a sequence of any number of non-numeric followed by a numeric.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

James Sabre wrote:I have to disagree with this. It is very easy to check for 4-32 alphanumerics and very easy to use a positive lookahead to look for a decimal digit. I would post the solution but won't until there is something that shows the OP has put some effort into this.


That's true. I was thinking that the problem was hard for arbitrary counts. For one, you are correct that lookahead is just fine. Which is the actual problem here.



If one specifies that there has to be 'n' or more decimal digits then one just needs a repeat count {n} inside the look ahead and allowing an arbitrary number of non-decimals between the decimals.
 
Jeanne Boyarsky
author & internet detective
Posts: 42103
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Sabre wrote:one just needs a repeat count {n} inside the look ahead and allowing an arbitrary number of non-decimals between the decimals.


Interesting. And cool!
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here you go

"(?=\\w{4,32})\\w*\\d+\\w*"

THANKS FOR THE INPUTS

by the way, i wanted the digit to appear anywhere in the alphanumeric value. For that reason, i had to use lookaround instead of lookahead and lookbehind

regular expressions are very interesting topic
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen gupta wrote:here you go

"(?=\\w{4,32})\\w*\\d+\\w*"



Did you test this with 33 or more characters? Try it with "x1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".


THANKS FOR THE INPUTS

by the way, i wanted the digit to appear anywhere in the alphanumeric value. For that reason, i had to use lookaround instead of lookahead and lookbehind



If you fix the problem I highlighted above I think you will find that 'look ahead' is easiest.


regular expressions are very interesting topic



Yep
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
33 characters is working fine

using lookahead, i have to write expression saying find so and so after this

but in my case single digit CAN appear anywhere in the string, first, middle or last of the string

So how does lookahead will help in my case?
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen gupta wrote:33 characters is working fine



But it should fail since you want at most 32 chars!


using lookahead, i have to write expression saying find so and so after this

but in my case single digit CAN appear anywhere in the string, first, middle or last of the string

So how does lookahead will help in my case?



It is trivial to fix your existing regex so that it rejects anything longer than 32. You are already using lookahead when checking the length is between 4 and 32 but the regex is more compact if you use lookahead to check for the decimal digit and not the length constraint. In the end it makes little difference so do whatever you are happy with as long as you fix the bug.
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James Sabre ------ SOMETIMES MY BRAIN STOPS WORKING FOR NO REASON

Anyway coming to the point, it is accepting more than 32 characters which i don't want

i am still trying to fix it. Let me know if you can give hint or solution itself.........

thanks
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me the optimal fix is to use lookahead for looking for the decimal digit and then match the 4-32 chars.
 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it worked, thanks a ton

i tried so many different ways. my mistake is i misunderstood the concept of lookahead

From your answer, i understand the concept of lookahead. which is more important. thanks again
 
Whatever you say buddy! And I believe this tiny ad too:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic