RegEx Solution Needed

M

Matt White

Hello,

I am in need of an effective regular expression that will capture
multiple values on the same line, not knowing in advance how many
matching values there are. Basically it needs to match strings of 3
digits within a string that contains anything (all on the same line).
I am pretty familiar with regular expressions but I've never tried
anything like this before.

So what I have is a line like:
abc123def456ghi789jkl

I want to end up with a list that looks like:
[123,456,789]

Thanks for your help!
 
S

Steven M. O'Neill

Matt White said:
So what I have is a line like:
abc123def456ghi789jkl

I want to end up with a list that looks like:
[123,456,789]

This looks pretty close:

~>perl -ne 'while (m/(\d{3})/g) {print "$1,"}'
abc123def456ghi789jkl
123,456,789,^C

Removing the final "," is left as an exercise for the reader.
 
J

John W. Krahn

Matt said:
Hello,

I am in need of an effective regular expression that will capture
multiple values on the same line, not knowing in advance how many
matching values there are. Basically it needs to match strings of 3
digits within a string that contains anything (all on the same line).
I am pretty familiar with regular expressions but I've never tried
anything like this before.

So what I have is a line like:
abc123def456ghi789jkl

I want to end up with a list that looks like:
[123,456,789]

$ perl -le'
$_ = q/abc123def456ghi789jkl/;

print for /(?<=\D)\d{1,3}(?=\D)/g;
'
123
456
789

$ perl -le'
$_ = q/abc123def456ghi789jkl/;

print for grep 3 == length, /\d+/g;
'
123
456
789




John
 
T

Tad McClellan

Matt White said:
I am in need of an effective regular expression that will capture
multiple values on the same line, not knowing in advance how many
matching values there are. Basically it needs to match strings of 3
digits
So what I have is a line like:
abc123def456ghi789jkl

I want to end up with a list that looks like:
[123,456,789]


use a global match in list context:

my @nums = 'abc123def456ghi789jkl' =~ /(\d\d\d)/g;
 
D

Dr.Ruud

Matt White schreef:
So what I have is a line like:
abc123def456ghi789jkl
I want to end up with a list that looks like:
[123,456,789]

echo abc123def456ghi789jkl |
perl -wnle 'print "[", join(",", /\d+/g), "]"'
 
J

Jürgen Exner

Matt said:
So what I have is a line like:
abc123def456ghi789jkl

I want to end up with a list that looks like:
[123,456,789]

$_ = 'abc123def456ghi789jkl';
my @res = split /\D+/;
print "@res";

jue
 
D

Dr.Ruud

Jürgen Exner schreef:
Matt White:
So what I have is a line like:
abc123def456ghi789jkl
I want to end up with a list that looks like:
[123,456,789]

$_ = 'abc123def456ghi789jkl';
my @res = split /\D+/;
print "@res";

There is a problem there which you will notice when you do

{
local $" = q{,};
print "[@res]";
}
 
D

Dr.Ruud

Dr.Ruud schreef:
Matt White:
So what I have is a line like:
abc123def456ghi789jkl
I want to end up with a list that looks like:
[123,456,789]

echo abc123def456ghi789jkl |
perl -wnle 'print "[", join(",", /\d+/g), "]"'

Variant:
echo abc123def456ghi789jkl |
perl -wnle '$"=","; print "[@{[/\d+/g]}]"'
 
B

Brad Baxter

Dr.Ruud schreef:
Matt White:
So what I have is a line like:
abc123def456ghi789jkl
I want to end up with a list that looks like:
[123,456,789]
echo abc123def456ghi789jkl |
perl -wnle 'print "[", join(",", /\d+/g), "]"'

Variant:
echo abc123def456ghi789jkl |
perl -wnle '$"=","; print "[@{[/\d+/g]}]"'

Golf:

echo abc123def456ghi789jkl |
perl -ple'$"=",";$_="[@{[/\d+/g]}]"'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top