Gossamer Forum
Home : General : Perl Programming :

regex help

Quote Reply
regex help
hello,

having brain freeze. let's say i have a string like this:

$this = "1234>ADS<5678##@@6787";

i want to pull out every instance of 4 digits in a row and store them somehow (array, whatever);

i must be overlooking something obvious, cause i keep trying stuff like this with no luck.

if ($this =~ m|(\d{4})|g) {
push(@ok, $1);
}
Quote Reply
Re: [adrockjames] regex help In reply to
Code:
my @array = $string =~ /\d{4}/g;
Quote Reply
Re: [Paul] regex help In reply to
thanks paul....for a follow up, let's say i have a string with random data in it, however patterns exist within that string that i want to pull out... like

$string = 'DDDDDEEEE9876<whatever_else>)DDEEECCC9866<whatever>)<other stuff>';

now, what i want is to pull out all instances of 4 digits followed by anything to the first instance of ')' like

@arr = qw /

9876<whatever_else>)

9866<whatever>)

/;

thanks again!
Quote Reply
Re: [adrockjames] regex help In reply to
Code:
my @array = ($string =~ /\d{4}.*?\)/g);

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] regex help In reply to
Code:
my @array = $string =~ /\d{4}[^)]*\)/g;

Saves using a non-greedy look-ahead.
Quote Reply
Re: [Paul] regex help In reply to
In Reply To:
Saves using a non-greedy look-ahead.
Meaning it is:

1. faster
2. uses less memory
3. anything else

?

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] regex help In reply to
It is just generally better because it was written by me.
Quote Reply
Re: [Paul] regex help In reply to
Code:
#!/usr/bin/perl

Paul->shut_up;


package Paul;

sub shut_up {
die "don't know anything useful to say anyway...."
}

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] regex help In reply to
s/know/have/;

I can correct your grammer if nothing else Laugh

Last edited by:

Paul: Dec 3, 2002, 11:38 AM