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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Leading Number Regex 2

Status
Not open for further replies.

jriggs420

Programmer
Sep 30, 2005
116
US
Hi all. I'm looking for the regex that seeks out a leading whole number (1,2,3...30) optionally followed be a period, and removes it. My original attempt was
Code:
s/^[1-30]\.?//;
not working tho. Suggestions? TIA

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
You might have to use something like:
[tt]
if($var+0>=1&&$var+0<=30)
{$var=~s/[0-9]+\.?//}
[/tt]
 
[1-30] is looking for the range 1 .. 3 (i.e. 1,2,3) or a zero. This isn't the most elegant but should do the trick:
Code:
s/
  ^             # start of the string
   (?:          # begin a non-capturing group
      [12]?\d   # an optional 1 or 2, followed by a digit (i.e. 1 to 29)
      |         # or
      30        # thirty
   )            # close the non-capturing group
   \.           # followed by a dot
//x;         # replace with nothing
(the /x at the end of the regexp allows me to add comments as I have done above, otherwise the regexp engine would be trying to match all the whitespace in my expression too).
 
Nice work Ish.
You beat me to that one.
Very clear and consise though.
Excellent work.

A Star from the Trojan! (very rare things)



Trojan.
 
Hi ish

Your regex would match a zero - on its own...

Sorry

Kind Regards
Duncan
 
Duncdude pointed out a minor error in your regex Ish.
You'll process a zero.
Maybe you might want to swap in something like this:
Code:
(?: [1-9] | [12]\d | 30 )

Trojan.
 
You're right Dunc - how's this:
Code:
s/
  ^              # start of the string
   (?:           # begin a non-capturing group
      [12]?[1-9] # an optional 1 or 2, followed by a digit between 1 and 9 (i.e. 1-9, 11-19, 21-29
      |          # or
      [1-3]0     # 1, 2 or 3 followed by zero (i.e. 10, 20, 30)
   )             # close the non-capturing group
   \.            # followed by a dot
//x;             # replace with nothing
Cheers TWB - rare indeed! Your profile says it's the ninth you've given in this forum - I'm privileged.
 
LOL!

Another nice solution Ish.
excellent lateral thinking.
I'd consider giving you another start but that might devalue Trojan Star's a little too much! ;-)



Trojan.
 
Thanks all...I haven;t had a chance to test it out yet, but I was wondering if Ishnid's latest post will account for the optional dot (.)? Does simply adding a ? after the character in question make it optional?

Also what is 'd', I know it's dataset, but what does that mean? is '23' a dataset? What about 'word' or '23word'? Many thanks-
Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Sorry, I missed that the dot at the end was optional - putting a '?' after it will indeed make it optional.

\d is a pre-defined character class - it matches any numeric character (i.e. 0-9). To define your own character classes, you enclose it in square brackets, e.g. [12] matches a '1' character or a '2' character.
 
I don't know.
I thought your work was good Ish.
Now it's been torn to pieces!.
Error after error.
I am SOOO dissapointed in you!!! ;-)


Trojan.
 
Yeah well you still can't have your star back . . . so there :p
 
I'll treasure it forever - it'll be like my own Phial of Galadriel.
 
Glad you're pleased with it!

Anyway, all joking aside, I did like your original post. It might not have been perfect but the concept was exactly what I would have liked to have posted.



Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top