Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Java RegEx Question

Posted on 2004-04-28
Medium Priority
396 Views
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
Question by:pcarrollnf
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2
• +1

LVL 14

Expert Comment

ID: 10943784
" 0 "
0

LVL 14

Expert Comment

ID: 10943785
Note the spaces on either side of the 0
0

LVL 86

Expert Comment

ID: 10943801
Pattern p = Pattern.compile("\\s+?0\\s+?");
Matcher m = p.matcher(input);
boolean gotLoneZero = m.find();
0

Author Comment

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

CEHJ earned 200 total points
ID: 10943864
Try

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

LVL 92

Expert Comment

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

LVL 14

Expert Comment

ID: 10943992
"(^0 | 0 | 0\$)"
0

LVL 86

Expert Comment

ID: 10944029
Identical to mine - only less tolerant of number of spaces
0

LVL 86

Expert Comment

ID: 10944146
8-)
0

LVL 92

Expert Comment

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

Identical but functions differently, interesting concept :-D
0

## Featured Post

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done withâ€¦
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used byâ€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month10 days, 10 hours left to enroll