CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
How to match a function of back reference?

 



kaza_perl
New User

Jun 6, 2013, 12:59 AM

Post #1 of 6 (12041 views)
How to match a function of back reference? Can't Post

Hello,

I'm attempting to match lines containing pairs of numbers
that have a mathematical relation between them, in the
simpliest form: the second is a decrement by 1 of the first.

For example:

some_string_1_more_string_0_last_string
some_string_2_more_string_1_last_string
some_string_3_more_string_2_last_string
some_string_4_more_string_3_last_string

How can I write a regex that after matching

m/^some_string_(\d+)_more_string_

will match a function of back reference "\1",
in this particular case: something like "\1-1" ?
I tried to search examples but so far
found none.

TIA,


BillKSmith
Veteran

Jun 6, 2013, 4:24 AM

Post #2 of 6 (12035 views)
Re: [kaza_perl] How to match a function of back reference? [In reply to] Can't Post

I would not expect a single regular expression to be able to do this. They only deal with characters. Numbers are just character strings, they have no numeric value. Your task is straightforward in perl. Just use two regular expressions.

Note: In your examples, you always find the larger number. Even a second regex would fail to find a match to a larger one.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jun 6, 2013, 11:21 AM

Post #3 of 6 (12026 views)
Re: [kaza_perl] How to match a function of back reference? [In reply to] Can't Post

Consider this session under the debugger:


Code
  DB<25> print $s; 
some_string_1_more_string_0_last_string

DB<26> ($c, $d) = $s =~ m/^some_string_(\d+)_more_string_(\d+)/;

DB<27> print "true! \n" if $c == $d + 1;
true!


Quite easy, isn't it?

But the calculation within the regex, I don't think you can do that.


(This post was edited by Laurent_R on Jun 6, 2013, 11:22 AM)


kaza_perl
New User

Jun 8, 2013, 8:46 PM

Post #4 of 6 (12002 views)
Re: [Laurent_R] How to match a function of back reference? [In reply to] Can't Post

Thanks for everyone who replied!

The whole /match/ regex is a part
of already written script so it places a limitation
of single-line regex. I hoped that there is some
(strange?) syntax allowing matching a function
of a back reference, in a similar way to a replacement
by a function of a match, like:

@{[${1}-1]}

but as I see from the replies, there isn't such a thing.
OK, either I'll have to use groups of 4 or 8 regex arguments
(for indices [1-4]->[0-3], [1-8]->[0-7], they're not
diverse) instead of a single regex argument I hoped to
come with or I'll have to declare in the script a hardcoded
associative array


Code
%q_dec = { 
'1','0',
'2','1',
'3','2',
'4','3',
'5','4',
'6','5',
'7','6',
'8','7',
};


and match values of it pointed by a back reference.
BTW, if the whole match regex is used like:


Code
if ($line_of_text =~ m/${regex}/) {...


can the $regex itself contain reference like:


Code
^\w+_([1-8])_\w+_${q_dec{\1}}_\w+


?

I attempted it, it didn't match, I attempted even declaring
a simple constant


Code
$a_const = "0";


and attempting to match only the "0" value like by having
the regex include:


Code
^\w+_([1-8])_\w+_${a_const}_\w+


but it still didn't match. Am I overlooking something with
the syntax?

TIA,


Laurent_R
Veteran / Moderator

Jun 9, 2013, 2:08 AM

Post #5 of 6 (11994 views)
Re: [kaza_perl] How to match a function of back reference? [In reply to] Can't Post

If this is what you are trying to do, this works (again a quick session under the Perl debugger):


Code
  DB<1>  $const = qr /foo/; 

DB<2> $d = "barfoobaz";

DB<3> print $1 if $d =~ /(bar${const}b)/
barfoob
DB<4>



kaza_perl
New User

Jun 12, 2013, 12:33 AM

Post #6 of 6 (11963 views)
Re: [Laurent_R] How to match a function of back reference? [In reply to] Can't Post

Thanks, Laurent_R.

It seems the problem I'm encountering is that
I'm attempting a nested interpolation of a Perl variable:
the whole regex to match is a Perl variable (set
from a command line argument) and matched like:


Code
if ($line =~ m/${match_regex}/) {...


and inside this variable $match_regex I'm attempting
to place a known-to exist another variable $const, so that
$match_regex looks like:


Code
^something_to_match${const}something_else_to_match$


and apparently, this nested interpolation doesn't work.
Am I trying to do something impossible like
double interpolation of a vriable in a match?

TIA,

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives