help on regex

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i want a reg exp for the below format..
1.0.00.0000

i tried as follows
\d\.\d\.\d{2}\.\d{4}

but it's accepting -ves numbers also(-1.0.00.0000)
I want only +ve numbers in my input..how do i check it?
 
Try this

^(?!\-)\+?\d\.\d\.\d{2}\.\d{4}

I have added the beginning of line ^ and the match invalidator (?! ) which
if the item in the validator matches, the match becomes invalid. Hence if
there is a minus sign, the match is invalid. (Also added \+? which allows for
an optional + sign.
 
Try this

^(?!\-)\+?\d\.\d\.\d{2}\.\d{4}

or if you dont want to enforce the digits, repeat the patter of \d\. such as

^(?!\-)\+?(\d+\.?)+

These concepts are added

^ Beginning of Line to anchor it.
(?! ) Match Invalidator. If the item within matches, it invalidates the
whole match.
in your case if a - is at the beginning then it will invalidate.
\+? says look for literal +, zero or 1 item. Just to be consistent.
 
AVL said:
i want a reg exp for the below format..
1.0.00.0000

i tried as follows
\d\.\d\.\d{2}\.\d{4}

but it's accepting -ves numbers also(-1.0.00.0000)
I want only +ve numbers in my input..how do i check it?

Well that will accept:

foobar fibbles foobar1.0.00.0000foobar fibbles foobar

You're not enforcing matching the beginning and end of the input. Try:

^\d\.\d\.\d{2}\.\d{4}$

Alun Harford
 
Back
Top