• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

Java RegEx Question

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
pcarrollnf
Asked:
pcarrollnf
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Tommy BraasCommented:
" 0 "
0
 
Tommy BraasCommented:
Note the spaces on either side of the 0
0
 
CEHJCommented:
Pattern p = Pattern.compile("\\s+?0\\s+?");
Matcher m = p.matcher(input);
boolean gotLoneZero = m.find();
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
pcarrollnfAuthor Commented:
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
 
CEHJCommented:
Try

("^0\\s+?|\\s+?0\\s+?|\\s+?0$");
0
 
objectsCommented:
Pattern p = Pattern.compile("\\s+?0\\s+?");
Matcher m = p.matcher(" "+input+" ");
boolean foundit = m.find();
0
 
Tommy BraasCommented:
"(^0 | 0 | 0$)"
0
 
CEHJCommented:
Identical to mine - only less tolerant of number of spaces
0
 
CEHJCommented:
8-)
0
 
objectsCommented:
> Identical to mine - only less tolerant of number of spaces

Identical but functions differently, interesting concept :-D
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now