Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: regex is not working as I intended

by ikegami (Patriarch)
on Jan 19, 2018 at 05:20 UTC ( #1207501=note: print w/replies, xml ) Need Help??

in reply to Re: regex is not working as I intended
in thread regex is not working as I intended

I'm not sure why,

At position 0,

  1. (?<=\')(.*?)(?>\') can't possibly match (there can't be a ' before the first character),
  2. (?<=\")(.*?)(?>\") can't possibly match (there can't be a " before the first character), but
  3. (.*) always matches.

Since it matched at position 0, it doesn't try to match at position 1 (where one of the first two alternates has a chance of matching).

Replies are listed 'Best First'.
Re^3: regex is not working as I intended
by fireblood (Scribe) on Jan 22, 2018 at 19:11 UTC
    I forgot to mention that this regex is used repeatedly on the same string to parse out parameter and value pairs, such as the following:

    radius = 3, density = .014, URL = "", max_no_of_attempts = 4

    That is why the lookbehind and lookahead assertions are used, even though they cannot possibly match at the beginning or end of the overall parameter string, they can match at intermediate positions as the regex is used to walk over the value of the parameter string parsing out individual parm and value pairs.

      Further to GrandFather's post:   Part of the useful enhanced context would be an expected hash structure for each multiple parameter/value pair string (hopefully, more than just one!) that you provide. E.g., for the multi-line string example here, do you expect

      %values = ( 'radius' => 3, 'density' => .014, 'URL' => '""', 'max_no_of_attempts' => 4, );
      or do you want something like
      %values = ( ..., 'URL' => '', ..., );
      (double-quotes stripped away)? How should wonky quoted strings be handled? By die-ing. By warn-ing and continuing to process? In another way?

      And yes, I think a module probably already exists to do this sort of thing.

      Give a man a fish:  <%-{-{-{-<

      Maybe you should create a new node with a bit more of the context for this problem. It sounds to me like there could be a call for a bigger hammer than just a single regex.

      Premature optimization is the root of all job security

      That doesn't change anything. The fixed version provided by Grandfather can be used to in parsing the snippet you provided.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2023-02-06 03:04 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.