469,275 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Regex capture between two strings.

I'm using this sub, to get the string between two places in a string, for example:

$result = GB($string, 'hi', 'there');

sub GB { return $1 if $_[0] =~ /@_[1](.*?)@_[2]/; }

I have a string, $target - when printed it is (5)
I want 5:

$target2 = GB($target, "(", ")" );

but when i print $target2, it always equals nothing
Jul 7 '07 #1
8 7954
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $target = '(5)';
  5. my $target2 = GB($target, "(", ")" );
  6. print $target2;
  7. sub GB {
  8.    return $1 if $_[0] =~ /\Q$_[1]\E(.*?)\Q$_[2]\E/;
  9. }
  10.  
because parenthesis are meta characters in a regexp, they have to be escaped otherwise they are treated like a capturing group: (.*?).

One way to do that is use the \Q..\E construct around those scalars in the regexp.

Also note @_[1] and @_[2] should be written in scalar context: $_[1] and $_[2] and not in list context like you had them.
Jul 7 '07 #2
Thanks alot, it worked.
Jul 8 '07 #3
I'm using this function to get the string between to parts of a string, for example:

$string = '<br>5<\br>'
$result = GB($string, '<br>', '<\br>')

That will give me 5. What do i do if i want to get more than one result from one string, for example:

$string = '<br>5<\br><br>6<\br><br>7<\br>'

How will i get 5,6 and 7 using my function, as they are wrapped around with the same strings?

Expand|Select|Wrap|Line Numbers
  1. sub GB {
  2.    return $1 if $_[0] =~ /\Q$_[1]\E(.*?)\Q$_[2]\E/;
  3. }
Jul 8 '07 #4
Once again, using this sub to get between two parts in a string

Expand|Select|Wrap|Line Numbers
  1. sub GB {
  2.    return $1 if $_[0] =~ /\Q$_[1]\E(.*?)\Q$_[2]\E/;
  3. }
Expand|Select|Wrap|Line Numbers
  1. $Health = '.' .GB($res, $Name .'</B></FONT><BR><TABLE><TR><TD><FONT color="' .$colour .'">', '</FONT>') .".";
  2. print "$Health\n";
I've checked, in $res:

Rohane(this is $Name)</B></FONT><BR><TABLE><TR><TD><FONT color="green(this is $colour)">49/50</FONT>

But $Health ends up as '..' not '.49/50.'
Jul 8 '07 #5
miller
1,089 Expert 1GB
Two threads merged as they relate to the same issue.

- MODERATOR
Jul 8 '07 #6
KevinADC
4,059 Expert 2GB
What you really need to do is stop and read some regexp tutorials. What you are doing looks like if could get more and more complicated. The general concept for getting all the matches is to use an array and the "g" modifier:

Expand|Select|Wrap|Line Numbers
  1. my @all_matches = $string =~ /(pattern)/g;
It might make your life much easier to use seperate functions, one for returning a string/scalar and one for when you want to return an array/list.

You may also want to look into using modules that are made for parsing HTML, the HTML class of module:

HTML::Parser
HTML::TableExtractor
etc
etc
Jul 8 '07 #7
I found another way round with the array \g part.

http://search.cpan.org/dist/HTML-Parser/Parser.pm

I googled HTML::Parser, though i'm not exactly sure how i'd use it.
Jul 8 '07 #8
KevinADC
4,059 Expert 2GB
I found another way round with the array \g part.

http://search.cpan.org/dist/HTML-Parser/Parser.pm

I googled HTML::Parser, though i'm not exactly sure how i'd use it.
I am not sure how you would use it either.
Jul 8 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Masahiro Ito | last post: by
10 posts views Thread by Chance Hopkins | last post: by
1 post views Thread by msnews.microsoft.com | last post: by
17 posts views Thread by clintonG | last post: by
17 posts views Thread by Mark | last post: by
1 post views Thread by =?Utf-8?B?QWxCcnVBbg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.