Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex only return the data that matches the regex

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello perl experts, I am hoping that someone can point me in the right direction.

I am running regex matches on a file which contains alpha numeric data. I am trying to figure out how to only match or print exactly what I am searching for instead of additional data.

For example.

Code:
while (<DATA>) {
    my $string = $_;
    if ($string =~ /5599686e3213df2d[a-z0-9]{10}/gi) {
        print $string, "\n";
    }
__DATA__
938483984zdkkl3993084083ljlj093840j304803840j5599686e3213df2dldj0483037408d0rh

Here is what I would like the output to look like
Code:
5599686e3213df2dldj0483037

Here is the output I am currently getting
Code:
938483984zdkkl3993084083ljlj093840j304803840j5599686e3213df2dldj0483037408d0rh
Not sure how go get only what I am matching on to print. The reason is that I am running some checks against the last {10} alphanumeric numbers and when I do those checks they aren't at the correct place because the $string is holding additional data.

 
$_ = '938483984zdkkl3993084083ljlj093840j304803840j5599686e3213df2dldj0483037408d0rh';
print "$1\n" if ( s/(5599686e3213df2d[a-z0-9]{10})/$1/ );
 
You want to use capturing parentheses [red]()[/red] :

Code:
while (<DATA>) {
    my $string = $_;
    if ($string =~ /[red]([/red]5599686e3213df2d[a-z0-9]{10}[red])[/red]/i) {
        print $1, "\n";
    }
__DATA__
938483984zdkkl3993084083ljlj093840j304803840j5599686e3213df2dldj0483037408d0rh

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
oops, messed that up, here it is again.....

You want to use capturing parentheses [red]()[/red] :

Code:
while (<DATA>) {
    my $string = $_;
    if ($string =~ /[red]([/red]5599686e3213df2d[a-z0-9]{10}[red])[/red]/i) {
        print $1, "\n";
    }
__DATA__
938483984zdkkl3993084083ljlj093840j304803840j5599686e3213df2dldj0483037408d0rh

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top