PDA

View Full Version : Regex - select a single digit as long as its not in a pair



pmorton
January 29th, 2010, 12:26 AM
I'm scratching my head over this, with my meagre regex skills. Can anyone help?

Embedded somewhere in a line of text is either a single digit, or a pair of digits. I want to match only the single digit. I'm probably over-complicating it when I say that I could do it with a lookforward and a lookbehind to negate a digit in either location, and I don't even think you can do both in one expression (can you?)

Please, some regex wizard, put me out of my misery with the trivial solution I'm missing!

Thanks

myrtle1908
January 29th, 2010, 12:54 AM
You could use word boundary for that eg.



use strict;
use warnings;

my @s;
while (<DATA>) {
@s = ($_ =~ /\b\d\b/g);
}
print join(':', @s);

__DATA__
3 4 hello 55 6 7 999999 there

Yields ...


3:4:6:7

pmorton
January 29th, 2010, 03:31 PM
You could use word boundary for that eg.



use strict;
use warnings;

my @s;
while (<DATA>) {
@s = ($_ =~ /\b\d\b/g);
}
print join(':', @s);

__DATA__
3 4 hello 55 6 7 999999 there

Yields ...


3:4:6:7


Thanks for that. It's a little more complicated though. How would it match the 3's in the following:

__DATA__
7 4 h3ll0 55 6 7 999999 th3r3[/CODE]

cdiem
January 29th, 2010, 06:02 PM
I have been trying to learn python in the last 2-3 days; except for the problems in the 'Project Euler' website, I thought this would be an interesting challenge to solve with the language. You could try with the following:



import re

DATA = "7 4 h3ll0 55 6 7 999999 th3r3[/CODE]"
print(re.findall(r"(?<![0-9])\d(?![0-9])", DATA))


On my machine:


python test.py
['7', '4', '3', '0', '6', '7', '3', '3']


The groups starting with "?<!" and "?!" are negative searches (as described here: http://docs.python.org/library/re.html).

myrtle1908
January 29th, 2010, 06:08 PM
use strict;
use warnings;

my $s = '7 4 h3ll0 55 6 7 999999 th3r3';
my @ss = ($s =~ m/(?<!\d)\d(?!\d)/g);
print join ':', @ss;


7:4:3:0:6:7:3:3

Is that what you want?

pmorton
January 29th, 2010, 06:43 PM
Yes, each is precisely what I want. Many thanks, both of you.