Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to enforce match priority irrespective of string position

by rsFalse (Chaplain)
on Mar 07, 2021 at 13:33 UTC ( [id://11129265]=note: print w/replies, xml ) Need Help??


in reply to How to enforce match priority irrespective of string position

>> I need to specify a priority of match without regard to position.

Try look-ahead search. May this sketch give some help:
$line =~ s/^ ( (?= .*? $regex_1 ) .*? $regex_1 #FIRST PRIORITY | (?= .*? $regex_2 ) .*? $regex_2 #SECOND PRIORITY ) /something/gmx;
Edit: removed text that caret is obsolete.

Upd.: I think my example (now striked-thourgh) simply reduces to the same but without look-ahead; see comment by Lanx.

Replies are listed 'Best First'.
Re^2: How to enforce match priority irrespective of string position
by LanX (Saint) on Mar 07, 2021 at 13:43 UTC
    > (?= .*? $regex_1 ) .*? $regex_1

    does it make sense to repeat the regex?

    isn't it rather

    (?= $re_cond_1 ) $re_match_1

    ?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Of course, '$re_cond_1' may be not equal to '$re_match_1'. But I wanted to show the simplest example. Further '\1' can be used to avoid self-repeating.
        > Of course, $re_cond_1 may be not equal to $re_match_1

        my point is, that it has IMHO no effect at all if it's identical.

        > But I wanted to show the simplest example

        too simple examples can confuse :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      I must say that this syntax confuses me. I am already using a lookahead to define the forward edge of the match (versus where the next match will start in the global substitution), and everything up to but not including that lookahead needs to be captured. I've never thought one could capture from a lookahead...but perhaps I'd misunderstood. I'm also using backslash lookaround assertions, because some of what is matched will be matched again (these are the false positives) and for an unpredictable number of times (fewer than 20).

      I tried putting rsFalse's suggestion to use but was unable to get the match to succeed. I don't think I understand it well enough.

      Blessings,

      ~Polyglot~

        you can separate the "condition to match" (in the lookahead) from the actual match-pattern.

        Point is that a failed partial match might move the pos forward, but if the condition fails pos won't be changed before trying the next or-alternative.

        rsfalse might provide/link an example.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-18 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found