Your skill will accomplishwhat the force of many cannot PerlMonks

Comment on

 Need Help??

Read the paragraph you quoted above very carefully.....

The regex engine works something like this: it moves from left to right, checking each alternation in order before moving on to the next position in the string.

Now, lets consider the pattern /(a|b|c)/ against the string 123abc. In the following diagram '^' denotes the current position in the string. The "pointer" gets moved one char to the right after each stanza:

```(\$txt = '123abc') =~ /(a|b|c)/;

1. ^123abc  - check for a -> fail
- check for b -> fail
- check for c -> fail

2. 1^23abc  - check for a -> fail
- check for b -> fail
- check for c -> fail

3. 12^3abc  - check for a -> fail
- check for b -> fail
- check for c -> fail

4. 123^abc  - check for a -> succeess, \$1 becomes 'a'
Here are a few others, to illustrate the point.
```(\$txt = 'barefoot') =~ /(foo|foot)/;

1. ^barefoot  - check for foo  -> fail
- check for foot -> fail

2. b^arefoot  - check for foo  -> fail
- check for foot -> fail

3. ba^refoot  - check for foo  -> fail
- check for foot -> fail

4. bar^efoot  - check for foo  -> fail
- check for foot -> fail

5. bare^foot  - check for foo  -> success, \$1 becomes 'foo'
Here, 'foo' beats 'foot' because they match at the same spot in the string, and foo is listed first in the alternation. The leftmost match will always win, the order they are listed in the alternation is merely a tie breaker.

And the one in question (slightly shortened)..

```(\$txt = 'arvec') =~ /(ar|ec|vec)\$/

1. ^arvec  - check for ar\$  -> fail (because of the end-of-string anch
+or)
- check for ec\$  -> fail
- check for vec\$ -> fail

2. a^rvec  - check for ar\$  -> fail
- check for ec\$  -> fail
- check for vec\$ -> fail

3. ar^vec  - check for ar\$  -> fail
- check for ec\$  -> fail
- check for vec\$ -> success, \$1 becomes 'vec'
Notice how 'ec' *would* match in the next stanza ("arv^ec"), but we never get that far. The first match wins, and in this situation it is the one we want.

Now, go back and read that paragraph you quoted again... does it make more sense now?

-Blake

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

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 2018-05-28 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (199 votes). Check out past polls.

Notices?