Solved

Java RegEx Question

Posted on 2004-04-28
10
389 Views
Last Modified: 2010-03-31
I want to build a regular expression that will let me know if a standalone 0 is in the string.  For example, I have the following string:

1 3 10 88 100 0 56
OR
0 4 10 20 45 1000
OR
2 10 44 60 100 0

The regular expression should return true on a match for a standalone 0.  However the string below should return false:

10 35 40 100

Thanks.
0
Comment
Question by:pcarrollnf
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10943784
" 0 "
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10943785
Note the spaces on either side of the 0
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10943801
Pattern p = Pattern.compile("\\s+?0\\s+?");
Matcher m = p.matcher(input);
boolean gotLoneZero = m.find();
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:pcarrollnf
ID: 10943842
That doesn't appear to work.  You have to also keep in mind that the 0 may be the first digit in the string where it will not have any leading spaces and it may be the last digit where it will not have trailing spaces.  It also may be a part of another number such as 10 where it is valid.  So I guess there are 3 scenarios that should fail:

0 1 2
1 2 0
1 0 2

Each number will have a space before the next number unless it is the last one in the string.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 10943864
Try

("^0\\s+?|\\s+?0\\s+?|\\s+?0$");
0
 
LVL 92

Expert Comment

by:objects
ID: 10943961
Pattern p = Pattern.compile("\\s+?0\\s+?");
Matcher m = p.matcher(" "+input+" ");
boolean foundit = m.find();
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10943992
"(^0 | 0 | 0$)"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10944029
Identical to mine - only less tolerant of number of spaces
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10944146
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 10944149
> Identical to mine - only less tolerant of number of spaces

Identical but functions differently, interesting concept :-D
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
How to convert from xls to xlsx using java 7 55
sql import cannot be resolved jsp 3 48
junit as external jar or library 7 23
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question