|
Posted by Ted Zlatanov on January 24, 2008, 10:41 am
Please log in for more thread options
R> smallpond schreef:
>> kirknew2Reg:
>>> I have a column that contains "suite 111" and "suite222"I need a $
>>> variable containing the word part and aother $ variable containing
>>> the digit part. I have tried variations on this syntax:
>>> (\w*)(\d*)(.*)
>>> (\w*)(\s?)(\d*)(.*)
>>> But nothig I have tried seperates the word from the digits when
>>> there is no space. How do i get 'suite222' to brake in to seperate
>>> variables?
>>
>> How about: /([[:alpha:]]*)\s*(\d*)/
R> To keep up the POSIX-style:
R> /([[:alpha:]]+)[[:blank:]]*([[:digit:]]+)/
R> And [[:blank:]] contains less characters that \s.
R> And [[:alpha:]] can of course be obscured as [^\W\d_].
Just make the \w match non-greedy. The last test case below is
questionable, but the OP didn't specify what to do in that case.
There's also the "match against the reversed string and reverse the
matches" approach :)
Ted
for ("suite 111", "suite222", " 111", "222")
{
if (/^(\w+?)\s*(\d+)$/) # match \w characters conservatively
{
print "$_: $1 - $2\n";
}
else
{
print "$_: no match\n";
}
}
-->
suite 111: suite - 111
suite222: suite - 222
111: no match
222: 2 - 22
|