Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match for zeros

Tags:

regex

I am looking for a regex pattern that would match several different combinations of zeros such as 00-00-0000 or 0 or 0.0 or 00000

Please help

Thanks!

EDIT:

Well, I have web service that returns me a result set, based on what it returns me I can decide if the result is worth displaying on the page. So if I get either 00-00-0000 or 0 or 00000 I would assume that data was not found, however if it brings back 001 or 000123 or 0356.00 - 1000 or 0.6700, this would be valid.

Hope this clarifies my question

Thanks

like image 421
Alex Avatar asked Nov 28 '08 02:11

Alex


People also ask

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What is used for zero or more occurrences in regex?

A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used. A regular expression followed by a plus sign ( + ) matches one or more occurrences of the one-character regular expression.

What is a zero-length match?

A zero-width or zero-length match is a regular expression match that does not match any characters. It matches only a position in the string. E.g. the regex \b matches between the 1 and , in 1,2. Zero-lenght matches are often an unintended result of mistakenly making everything optional in a regular expression.

What do you mean by O or 1 character?

When a character is followed by ? in a regular expression it means to match zero or one instance of the character. So X? matches an 'X' if there is one in the string, but otherwise matches nothing.


4 Answers

You need to better define what is valid to appear between the zeros. Going from your question, I'll assume you're looking for any number of zeros with any number and grouping of "-" and "." between them....

0([-.]?0+)*

Hope you don't mind, SoapBox:

Based on the question edit, what you are looking for is any string that has non-zero digits in it, so:

[1-9]

or, if the regex engine automatically anchors start and end of string:

.*[1-9].*

may be the better solution.

This is the reverse of the test you asked for but that's a simple matter to change (reverse the sense of the if-statement).

like image 169
SoapBox Avatar answered Oct 26 '22 11:10

SoapBox


[^123456789]+

or

[^1-9]+

I believe this is what you are searching for...

like image 22
user39307 Avatar answered Oct 26 '22 12:10

user39307


Here's the Regex that will match what you have asked for so far. If there is more you want it to match, please specify.

0+((\.0+)|(-0+)*)

That matches all of the examples you asked for.

like image 35
Timothy Khouri Avatar answered Oct 26 '22 11:10

Timothy Khouri


I use this one, starts and ends with zero and contains only zeros.

^[0]+$
like image 20
Leandro S Avatar answered Oct 26 '22 10:10

Leandro S