• 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 Below Strings

 
Greenhorn
Posts: 28
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Strings are having below
i.e every string contains 6 characters and then space separator added;

X means any number between 1 to 30

1)0 0 0 0 0 0
2)X 0 0 0 0 0 EX)22 0 0 0 0 0
3)0 0 0 0 0 X EX)0 0 0 0 0 20
4)0 X 0 X 0 0 EX)0 10 0 22 0 0


so how find that exact patterns are executed ,
i'm fully confused please give me the summarised solution
thanks in advance
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please KeepItDown. You can edit your post to fix that:
 
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
Since you are "fully confused" you should spend some time learning about regular expressions. A good site to start is http://www.regular-expressions.info/tutorial.html .

P.S. I don't understand what you want to do. Are you trying to verify the pattern is correct, extract the fields or what?
 
jagadeesh indukuri
Greenhorn
Posts: 28
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Please KeepItDown. You can edit your post to fix that:



thank you for your response
 
jagadeesh indukuri
Greenhorn
Posts: 28
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:Since you are "fully confused" you should spend some time learning about regular expressions. A good site to start is http://www.regular-expressions.info/tutorial.html .

P.S. I don't understand what you want to do. Are you trying to verify the pattern is correct, extract the fields or what?



ok i follow above link
 
jagadeesh indukuri
Greenhorn
Posts: 28
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tested below expression but i.e gives result is true but given value is not greater than 7 then how can i check by using regex
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Fred always says, turn your computer off.
As I always say, get a pencil paper and eraser. Write down carefully what your regex matches.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The regular expression [1-7]+ means: one or more digits in the range 1 to 7. Your string contains "14" - two digits that are both in the range 1 to 7. So, ofcourse, this matches the regular expression.

As others already noted, please explain exactly what you want - if you don't explain it exactly, it's hard to give you a useful answer.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find that when i want to create a regex, to build it up a piece at a time, writing it out in English. for example...

The beginning of a line, followed by
the literal "ABC", followed by
one or more numeric digits (0-9), followed by
zero or more characters of any kind.

Once I have this, building the regex itself is easy.

So, think about what EXACTLY you want to allow, and how you'd explain to a 10-year old child what is allowed and what is not in your matching.
 
jagadeesh indukuri
Greenhorn
Posts: 28
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for giving response but,

My requirement is not allowed more than 31 number value and also not consider 0 value for regex

that regex matches true then it is below 31 number otherwise i.e greater than 31 or zero itself
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, you want to check if the input is a number in the range 1 to 31?

Regular expressions aren't very suitable for checking if a number is in a certain range. It's probably much easier and much more clear to parse the string into an int, and then check if it is within the range with a simple if-statement:
 
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

Jesper de Jong wrote:So, you want to check if the input is a number in the range 1 to 31?

Regular expressions aren't very suitable for checking if a number is in a certain range. It's probably much easier and much more clear to parse the string into an int, and then check if it is within the range with a simple if-statement:



For a single isolated number then I agree with this but when one needs to check that each number in a string is in the range 1-31 then using a regular expression can make sense. A basic regular expression for a single number term is [...solution removed...] and one can use this repeatedly to build up a regular expression for the whole input string (I take this approach for validating an IP address). I will let the OP take it from here.

The biggest problem I have with this whole thread is that it is not clear to me what the task is. Does the OP want to prove that an input string has a particular syntax? Does he want to split out the numbers and if so does he want them as a String or as a number? Are leading zeros allowed (i.e. does "000001" represent a valid number)?
 
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

jagadeesh indukuri wrote:thanks for giving response but,
My requirement is not allowed more than 31 number value and also not consider 0 value for regex
that regex matches true then it is below 31 number otherwise i.e greater than 31 or zero itself


OK, so (assuming you don't want to take Jesper's excellent advice), think about what the properties of a number in the range 1-31 are:
  • What's the maximum number of digits can it have?
  • What's the minimum number of digits can it have?
  • What are the rules for the first digit?
  • How does the first digit affect other digits that follow it?

  • Nobody here is simply going to hand you the answer (and if they do, it will be removed); you need to think about the problem for yourself.

    Try out some patterns, and ask yourself if they cover ALL the rules for a number between 1 and 31. If you're still having problems, come back with your work, and we'll try and help you some more.

    Winston
     
    Winston Gutkowski
    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

    Richard Tookey wrote:A basic regular expression for a single number term is...


    Richard,

    I've removed your solution because we're trying to get OP to think about it for himself. If jagadeesh does ShowSomeEffort and still has problems, then might be the time to offer a solution (with a full explanation as to why it works), but not yet, I think.

    Winston
     
    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

    Winston Gutkowski wrote:

    Richard Tookey wrote:A basic regular expression for a single number term is...


    Richard,

    I've removed your solution because we're trying to get OP to think about it for himself. If jagadeesh does ShowSomeEffort and still has problems, then might be the time to offer a solution (with a full explanation as to why it works), but not yet, I think.

    Winston



    I'm uncomfortable with this. I deliberately only provided a part of the overall regular expression that I suspect is needed. It is obvious that the OP needs to do more than just check one number and at the moment he can't see the wood for the trees. Jesper provided a partial solution but to what I suspect is not the problem the OP is trying to solve and is probably going to confuse the OP more than help him! I thought it was necessary to try to direct the OP in the direction that seemed more appropriate without actually giving him the full solution. My experience is that in general to just keep pointing any OP at the regex tutorial is counter productive and that giving an OP a regex fragment as a lever gives him the impetus to take it further himself.

     
    Winston Gutkowski
    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

    Richard Tookey wrote:I deliberately only provided a part of the overall regular expression that I suspect is needed.


    Yes, but it was the specific part that we've been trying to get him to think about.

    I'm not going to get into a big argument about it, so if you really feel you must provide it, I won't stop you; but I do think that that if you do so, you should provide a complete explanation of why it works so that jagadeesh can understand what he's seeing.

    Winston
    reply
      Bookmark Topic Watch Topic
    • New Topic