in reply to Re^6: Regex match at the beginning or end of string
in thread Regex match at the beginning or end of string

Like I said, bad choices don't make good examples \w also matches '8'.

Correct that and it is easy to see how to determine if the matches were consecutive from @- * @+

"&8" =~ /^(?=.*([A-Z]))(?=.*(\d))/ and print "[@-][@+]";; "A8" =~ /^(?=.*([A-Z]))(?=.*(\d))/ and print "[@-][@+]";; [0 0 1][0 1 2] "A+8" =~ /^(?=.*([A-Z]))(?=.*(\d))/ and print "[@-][@+]";; [0 0 2][0 1 3]

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^8: Regex match at the beginning or end of string
by JavaFan (Canon) on Feb 20, 2011 at 15:25 UTC
    Well, duh. If the patterns cannot overlap, it's easy and your suggestion, or my splitting into different regexes work fine. No checking of @- and @+ is needed.

    Of course I know \w matches 8. That was the point of the example. Both /^(?=.*\w)(?=.*\d)/ and /\w/ && /\d/ break down on this.

      Of course I know \w matches 8. That was the point of the example.

      Then I'm sorry, but I still don't understand the point you are making?

      Using one regex that matches both required components, to check if the string contains both components just doesn't make any sense. To me at least.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        The OP has several patterns he wants to match - in a non-overlapping way. In particular, he wants to find out if both matches follow each other directly. There's no reason to assume the patterns cannot (partially) overlap (which is where your suggestion breaks down). Using \w and \d are two simple patterns than can match in an overlapping way. Translated into the OPs problem "check if a word character is followed by a digit, or a digit followed by a word character". "A8", "8A" and "89" are all acceptable. "&8" isn't.