Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Regular Expression: search two times for the same number of any signs

by hippo (Bishop)
on Nov 29, 2016 at 10:06 UTC ( [id://1176776]=note: print w/replies, xml ) Need Help??


in reply to Regular Expression: search two times for the same number of any signs

If I understand you correctly (and maybe not, see How to ask better questions using Test::More and sample data) you want this:

  1. Find the position of the first 'x'.
  2. Find the position of the second 'x'.
  3. Take the difference in these positions, add it to the second position and look here for the third 'x'.
  4. If all that succeeds you have a match.

In which case, just code this up with a loop and judicious use of index and substr.

However, this does sound rather like an XY Problem. Perhaps if you explained why you want to do this in the first place a much better solution might become apparent.

  • Comment on Re: Regular Expression: search two times for the same number of any signs

Replies are listed 'Best First'.
Re^2: Regular Expression: search two times for the same number of any signs
by Ratazong (Monsignor) on Nov 29, 2016 at 10:19 UTC

    Hi hippo

    Unfortunately, this approach violates the spec: the second 'x' might qualify for a "." ("any sign"), and might not indicate the middle of the string.

    So long, Rata

      If that's really the case (and you are probably right) then maybe change the order: find the first and last occurrence of 'x', calculate where the middle one should be and look there?

      However, the spec is a little woolly and the whole thing is still shouting "XY!" at me.

Re^2: Regular Expression: search two times for the same number of any signs
by Anonymous Monk on Nov 29, 2016 at 10:28 UTC

    Yes. You understand my problem perfectly.

    This is also an option. To search for this pattern with a small program. (multiple searchs). The question was, can I do it also with a single regular expression. Using predefined variables like $1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176776]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2024-04-23 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found