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
8 8235 - use strict;
-
use warnings;
-
-
my $target = '(5)';
-
my $target2 = GB($target, "(", ")" );
-
print $target2;
-
sub GB {
-
return $1 if $_[0] =~ /\Q$_[1]\E(.*?)\Q$_[2]\E/;
-
}
-
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.
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? - sub GB {
-
return $1 if $_[0] =~ /\Q$_[1]\E(.*?)\Q$_[2]\E/;
-
}
Once again, using this sub to get between two parts in a string - sub GB {
-
return $1 if $_[0] =~ /\Q$_[1]\E(.*?)\Q$_[2]\E/;
-
}
- $Health = '.' .GB($res, $Name .'</B></FONT><BR><TABLE><TR><TD><FONT color="' .$colour .'">', '</FONT>') .".";
-
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.'
Two threads merged as they relate to the same issue.
- MODERATOR
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: - 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
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 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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:
...
|
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...
|
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....
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |