smanicka has asked for the wisdom of the Perl Monks concerning the following question:

Could anyone help me with a regex which identifies if a phrase begins with the word "Ascii" (case insensitive)?
  • Comment on regex for a phrase beginning with some chars

Replies are listed 'Best First'.
Re: regex for a phrase beginning with some chars
by kennethk (Abbot) on Feb 18, 2009 at 20:36 UTC
    A good general resource for crafting regexes is perlretut and is worth a read through. For your case as described, you could use /^ascii/i. This expression does the following:
    1. Starts at the beginning of a phrase ( /^ )
    2. Matches the letters 'ascii' in order ( ascii )
    3. Runs the matching in a case insensitive fashion ( /i )
Re: regex for a phrase beginning with some chars
by tirwhan (Abbot) on Feb 18, 2009 at 20:37 UTC

    perldoc perlre, the metacharacter "^" is your friend (or if you're PBP-inclined use "\A"). Then progress backwards to the "i" modifier and you're done.


    All dogma is stupid.
Re: regex for a phrase beginning with some chars
by toolic (Bishop) on Feb 18, 2009 at 20:41 UTC
    The \b anchor may also be useful, depending on how you define a "word":
    use strict; use warnings; while (<DATA>) { print if /^ascii\b/i; } __DATA__ does not begin with ascii ascii is the one AsCii works too asciibetically sorted

    Prints:

    ascii is the one AsCii works too

    Updated: Added "asciibetically" line to demonstrate \b (johngg++)

      I think your application of the \b anchor might better have been demonstrated with a string like "asciibetically sorted" which would show that there is no word boundary after the "ascii" part.

      Cheers,

      JohnGG