473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 8235
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

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

Similar topics

4
by: Masahiro Ito | last post by:
I have attached a block of text similar to the type that I am working with. I have been learning a lot about Regex - it is quite impressive. I can easily capture bits of info, but I keep having...
2
by: Jose | last post by:
There's something for me to learn with this example, i'm sure :) Given this text: "....." and my first attempt at capture the groups: "(?:\)" RegExTest gives me what i expect: 6 captured...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
10
by: Chance Hopkins | last post by:
I'm trying to match a set of matches after some initial text: mytext: "something" "somethingelse" "another thing" "maybe another" (?:mytext: )(?<mymatch>{1,1}+{1,1}+)+ I only get the last one...
1
by: msnews.microsoft.com | last post by:
Hi, I have the expression "<font+>""(*)""</font>+\?AUTHOR_ID=+"">(*)</a>" Any body can tell me what is the meaning of that expression and what is the output of the expression. Regards, Muhammad...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
3
by: | last post by:
I'm analyzing large strings and finding matches using the Regex class. I want to find the context those matches are found in and to display excerpts of that context, just as a search engine might....
1
by: =?Utf-8?B?QWxCcnVBbg==?= | last post by:
I have a regular expression for capturing all occurrences of words contained between {{ and }} in a file. My problem is I need to capture what is between those symbols. For instance, if I have...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.