To the point: what I am looking for is a relatively clean way to say "any front anchored substring of $word" in regex.
If $word='ello', then

my $magicRegex = munge($word); if (/^h$magicRegex$/) {...}
should match 'h','he','hel', etc.


Background: (aka, XY-problem repellent)
I have a text-based combat game I'm working on, which involves the player sending commands such as

The latter causing the first three tubes to be pointed in the ship named raider's general direction plus 4 5 and 6 degrees for tube 1 2 and 3 respectively.

The backend physics code works fine, but the english parsing is not as flexible as it could be.

I've come up with a decent way to allow the parameters to be listed in any order, provided that the tags "dispersion" and "offset" are present to identify which number is which:

my $number = qr#(-?\d+\.?\d*)#; my $mixNmatch = qr#(?:(?:\s+[d][a-z]*\s+$number)|(?:\s+[oa][a-z]*\s+$n +umber))*#; while ($_ = <>) { no warnings; # dealing with undefs is clutter for this example print /^lock$mixNmatch\s*$/i ? "Yes: disp=$1, offset=$2\n" : "nope\n +"; }

The idea being that "d", "disp" and "dispersion" are all acceptable to mark a dispersion, while "offset", "angle", "ang", etc are all acceptable to mark an offset/angle. The ugly part is that "awoogha" also counts as marking an angle simply because it starts with an 'a'.

And thus I ask, is there a better way, or is this good enough?


The monks can fry your fish, and they can give you some tips and some bait, but you still need to wake up and climb onto the boat.

In reply to Regex - Matching prefixes of a word by SuicideJunkie

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.