Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick regular expression help needed 2

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
0
0
US
I have to test a year value to have 1 or 2 digits.
I was using \d{2}|\d{1}
The problem is the value comes in with two spaces at the end filling it in as a 4 character field.

All I need is a regular expression that says 1 or 2 digits followed by any or no non-digit characters.

I am actually doing this in a proprietary scripting language of an application I have to work in, not in Javascript so I cannot do other formatting, trimming, casting, etc of the value and need to do it in the regular expression.

Anyone?

At my age I still learn something new every day, but I forget two others.
 
No, 1 OR 2 digits followed by any number of non-digit characters.
For example the year could be represented by a single digit 8 for 2008 or as 08. The value is stored in an array of 4 characters though and apparently it populates the non-filled characters with spaces.

I have to determine if I have a year that is not the full four digits so that I can convert it to 4 digits. The scripting language I am in only allows me a positive match, I cannot use logical NOT.
So essentially I have to test for one digit followed by three (or more) non-digits OR 2 digits followed by two (or more) non-digits.
I'm just not that good with Regular Expressions yet and most of the examples I find use logical operators that wont work in this implementation. I can use | but not a logical NOT.


At my age I still learn something new every day, but I forget two others.
 
>All I need is a regular expression that says 1 or 2 digits followed by any or no non-digit characters.
>1 OR 2 digits followed by any number of non-digit characters

[tt] /^\d{1,2}\D*$/[/tt]

>I have to test a year value to have 1 or 2 digits.
>I was using \d{2}|\d{1}
>The problem is the value comes in with two spaces at the end filling it in as a 4 character field.

[tt] /^(\d{2}\x20{2})|(\d{1}\x20{3})$/;[/tt]
 
No luck. For some reason it returns true even if a 4 digit year is sent in. Tried many variations without success.
I finally had to write code to pull the value from the array, trim the spaces off and write it back into the array again so the spaces were not there when it did it's tests later.

Maybe it's just the way they have implemented regular expressions in this scripting language.

Thanks for the help guys.

At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top