This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line!
See this thread for details.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Regex help with beginning of string

 
Ranch Hand
Posts: 190
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best, forumists!

I need help with a regex that returns an error if it finds whitespace in the beginning of a string if that string is longer than at least 2 characters
and the string should only contain digits if it contains a non digit character the regex should return an error.

This task seems simple but keep in mind that my regex is not what I wish it would be. I am still a learner in that area.

I should mention that I use the string method matches to test the regex in code. I hope someone here can pinch me in the right direction.

Many thanks to all viewers!

Best regards,
Robert!
 
Saloon Keeper
Posts: 12606
273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please give us examples of strings that will validate and strings that will fail. From your description, I expect that the strings "5 ", " 7" and "  " will validate, while "a9", "1e" and "36 " wil fail, correct.

Show us what you've tried, and what you're stuck on. Also tell us why you think a regular expression is the solution, and not just a bunch of other method calls on your input string.
 
Marshal
Posts: 71655
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a section I like in the Java™ Tutorials. You want something like $ or ^ at the ends of your expression to represent line boundaries. Go through the tutorial and it will teach you about the Pattern and Matcher classes, which probably do a better job of matching than plain String.
 
Robert Ingmarsson
Ranch Hand
Posts: 190
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stephan Van Hulst

Valid strings in this situation would be

"1"
"01"
"23456"

Invalid strings would be like this

" 1"
"   2"
" 2dsds"

I haven't tried that much regex for this case, I am still only at a research level.
There is the non-regex methods "startsWith", "contains", "Character.isDigit" and so on that I have experimented with
but I believe regex is best suited here. Thanks for your reply!

Regards,
Robert!
 
Robert Ingmarsson
Ranch Hand
Posts: 190
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best all!

For example I have tried the code



But the code and regex sample i have supplied here above only tests if a strings starts with a whitespace character or not. I wan't to check
if a string does not starts with a whitespace and only contains numbers. Sorry if I am confusing anyone. Not my intention. Thanks for
your patience with me.

Regards,
Robert!
 
Robert Ingmarsson
Ranch Hand
Posts: 190
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, again!

I think I have found a solution to my problem here.



Some what hasty maybe. But I would have liked a single regex string for this situation. Sorry for my bad code.

Thanks for your visit,
Robert!



 
Saloon Keeper
Posts: 7608
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just
 
Campbell Ritchie
Marshal
Posts: 71655
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need the isEmpty() test? Surely \\s+ will pick that up?

[edit]This post turned out to contain a mistake, as you will see in the next post and here.
 
Carey Brown
Saloon Keeper
Posts: 7608
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Do you need the isEmpty() test? Surely \\s+ will pick that up?


What "\\s+" ? Mine didn't have one of these. It looked, from his previous code, that spaces were an error.
 
Carey Brown
Saloon Keeper
Posts: 7608
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 7608
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Saloon Keeper
Posts: 22992
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Let me take a stab. As I read it, the input MUST be exclusively digits EXCEPT that if there is only one digit, then it may be preceeded by one and ONLY 1 space.

So, in raw regex (omitting Java's extra escapes), that should be something like this:


Or any of several mix/match and variant forms of the above, including replacing the "+" and "*" with their numeric-range forms. Parentheses probably optional/replaced by a better equivalent (for example, "\s{0..1}", but this is just off the top of my head.

The \d\d is sort of redundant, but I did it to emphasis the fact that a simple \d also matches the \s*\d where the space is missing.
 
Carey Brown
Saloon Keeper
Posts: 7608
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:OK. Let me take a stab. As I read it, the input MUST be exclusively digits EXCEPT that if there is only one digit, then it may be preceeded by one and ONLY 1 space.

So, in raw regex (omitting Java's extra escapes), that should be something like this:


Based on your descriptive requirements it should be more like
I'm guessing that an empty text field just means that nothing has been entered yet and shouldn't be an error. Don't know if this is true or not without requirements.
 
Tim Holloway
Saloon Keeper
Posts: 22992
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm operating off this:


error if it finds whitespace in the beginning of a string if that string is longer than at least 2 characters



But now that you mention it, "\s*" is open-ended, unlike {0..1}. So a more accurate form absent the explicit range would be: Since the leading space is apparently optional.
 
Robert Ingmarsson
Ranch Hand
Posts: 190
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey Brown

I have written an example of what I am trying to accomplish. This is just a loose sample and has nothing to do with the application I am working on at the moment. But it gives an idea of what kind of a problem that I have encountered.



I hope that helps to see what's bothering me. My english is like my java, like everything else in my life, nothing is real, all is purely on a wannabe level  

Kind Regards,
Robert!

 


 
Robert Ingmarsson
Ranch Hand
Posts: 190
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim Holloway!

I tried your regex but it gave me an error exception as listed below



Best regards,
Robert!
 
Carey Brown
Saloon Keeper
Posts: 7608
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without requirements we're just guessing at your intentions. It's appearing like your regular expressions are incorrect and possibly your if() logic as well. If you are having difficulty describing it in english could you describe it in pseudo code? Example:
Not sure why you are trying to make special cases out of spaces.
 
Campbell Ritchie
Marshal
Posts: 71655
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:, , , What "\\s+" ? . . . .

Damn! I read it wrongly. Of course you wrote \\d+ and that would fail to match any spaces anywhere.
 
Tim Holloway
Saloon Keeper
Posts: 22992
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robert Ingmarsson wrote:@Tim Holloway!

I tried your regex but it gave me an error exception as listed below



Best regards,
Robert!



Did you escape the escape characters?

Remember, I wrote the actual regex, but to code it as a Java String literal, you need to replace "\"s with "\\".
 
Tim Holloway
Saloon Keeper
Posts: 22992
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Tim, you are also missing a paren.



Or have 1 too many. Was supposed to be:


The problem with seeing what's "supposed" to be there and not what actually is.
reply
    Bookmark Topic Watch Topic
  • New Topic