Click here to get back home

RegEx, how to seperate word from digits?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
RegEx, how to seperate word from digits? kirknew2Reg 01-23-2008
Get Chitika Premium
Posted by kirknew2Reg on January 23, 2008, 3:21 pm
Please log in for more thread options
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?

Posted by smallpond on January 23, 2008, 3:30 pm
Please log in for more thread options
> 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*)/



Posted by Dr.Ruud on January 24, 2008, 5:28 am
Please log in for more thread options
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*)/

To keep up the POSIX-style:

/([[:alpha:]]+)[[:blank:]]*([[:digit:]]+)/

And [[:blank:]] contains less characters that \s.

And [[:alpha:]] can of course be obscured as [^\W\d_].

--
Affijn, Ruud

"Gewoon is een tijger."

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

Posted by Florian Kaufmann on January 23, 2008, 4:12 pm
Please log in for more thread options
Similar to smallpond's answer, however enforces that the word- and the
digitpart are at least 1 character long. Else, also things like "3"
"x" or even the empty string "" are found.

my ($word,$digit) = /([[:alpha:]]+)\s*(\d+)/;
# do something with $word and $digit

Similar ThreadsPosted
regex matching exactly 10 digits November 28, 2006, 8:58 am
regex to match strings that don't contain any digits? August 9, 2005, 6:57 am
Regex matching a string that DOESN'T contain a given word August 31, 2004, 1:07 pm
Win32::OLE MS Word line breaks regex February 3, 2007, 6:43 pm
Installing seperate version of Perl. July 10, 2004, 8:38 am
Copy multiples files to a single file in seperate pages.. March 27, 2008, 3:30 pm
How to extract part of the text (htm) file after start word until end word? May 12, 2006, 5:32 am
regular expression for digits November 15, 2007, 4:36 am
Removing Comma Within Digits November 12, 2008, 8:30 am
Current Time with 5 digits of milliseconds May 12, 2008, 3:39 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap