undo the (?i)

Discussion in 'Perl Misc' started by joeri, Aug 8, 2003.

  1. joeri

    joeri Guest

    Hi all,

    I have an RE where I use the (?i) somewhere, but I want to undo that effect
    further on in the RE.
    Here's an example:

    $var="(?i)(forty|fifty)";
    $input = "Forty-ninth ST";
    if ($input =~ /$var(\-ninth)\s[S|s]t/) {
    print "$&\n";
    }

    Because the $var contains the (?i), it matches the rest of the RE
    case-insensitively, but I want
    to match everything after the $var case-sensitively. Can I escape the ($i)
    or set the scope of it?

    Thanks,

    J
     
    joeri, Aug 8, 2003
    #1
    1. Advertising

  2. Also sprach joeri:

    > I have an RE where I use the (?i) somewhere, but I want to undo that effect
    > further on in the RE.
    > Here's an example:
    >
    > $var="(?i)(forty|fifty)";
    > $input = "Forty-ninth ST";
    > if ($input =~ /$var(\-ninth)\s[S|s]t/) {
    > print "$&\n";
    > }
    >
    > Because the $var contains the (?i), it matches the rest of the RE
    > case-insensitively, but I want
    > to match everything after the $var case-sensitively. Can I escape the ($i)
    > or set the scope of it?


    You can again turn it off at some point:

    $input =~ /$var(?-i)(-ninth)\s[S|s]t/;

    So, -i will prevent case-insensitive matching for the patterns right of
    it.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Aug 8, 2003
    #2
    1. Advertising

  3. Also sprach joeri:

    > Tassilo v. Parseval wrote:
    >
    >> > I have an RE where I use the (?i) somewhere, but I want to undo
    >> > that effect further on in the RE.
    >> > Here's an example:
    >> >
    >> > $var="(?i)(forty|fifty)";
    >> > $input = "Forty-ninth ST";
    >> > if ($input =~ /$var(\-ninth)\s[S|s]t/) {
    >> > print "$&\n";
    >> > }
    >> >
    >> > Because the $var contains the (?i), it matches the rest of the RE
    >> > case-insensitively, but I want to match everything after the $var
    >> > case-sensitively. Can I escape the ($i) or set the scope of it?

    >>
    >> You can again turn it off at some point:
    >>
    >> $input =~ /$var(?-i)(-ninth)\s[S|s]t/;
    >>
    >> So, -i will prevent case-insensitive matching for the patterns right of
    >> it.


    > Thanks! I couldn't find it in the docs or google.


    You'll find it in perlre.pod under "Extended patterns":

    "(?imsx-imsx)"
    One or more embedded pattern-match modifiers, to be turned on
    (or turned off, if preceded by "-") for the remainder of the
    pattern or the remainder of the enclosing pattern group (if
    any). [...]

    It can indeed be easily overlooked.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Aug 8, 2003
    #3
  4. joeri

    joeri Guest

    Tassilo v. Parseval wrote:

    > Also sprach joeri:
    >
    > > Tassilo v. Parseval wrote:
    > >
    > >> > I have an RE where I use the (?i) somewhere, but I want to undo
    > >> > that effect further on in the RE.
    > >> > Here's an example:
    > >> >
    > >> > $var="(?i)(forty|fifty)";
    > >> > $input = "Forty-ninth ST";
    > >> > if ($input =~ /$var(\-ninth)\s[S|s]t/) {
    > >> > print "$&\n";
    > >> > }
    > >> >
    > >> > Because the $var contains the (?i), it matches the rest of the RE
    > >> > case-insensitively, but I want to match everything after the $var
    > >> > case-sensitively. Can I escape the ($i) or set the scope of it?
    > >>
    > >> You can again turn it off at some point:
    > >>
    > >> $input =~ /$var(?-i)(-ninth)\s[S|s]t/;
    > >>
    > >> So, -i will prevent case-insensitive matching for the patterns right of
    > >> it.

    >
    > > Thanks! I couldn't find it in the docs or google.

    >
    > You'll find it in perlre.pod under "Extended patterns":
    >
    > "(?imsx-imsx)"
    > One or more embedded pattern-match modifiers, to be turned

    on
    > (or turned off, if preceded by "-") for the remainder of the
    > pattern or the remainder of the enclosing pattern group (if
    > any). [...]
    >
    > It can indeed be easily overlooked.
    >


    Here's what my 5.6 docs say. It is indeed there, but I overlooked it...

    (?imsx-imsx)
    One or more embedded pattern-match modifiers. This is particularly useful
    for dynamic patterns, such as those read in from a configuration file, read
    in as an argument, are specified in a table somewhere, etc. Consider the
    case that some of which want to be case sensitive and some do not. The case
    insensitive ones need to include merely (?i) at the front of the pattern.
    For example:
    $pattern = "foobar";
    if ( /$pattern/i ) { } # more flexible: $pattern = "(?i)foobar";
    if ( /$pattern/ ) { }Letters after a - turn those modifiers off.

    J
     
    joeri, Aug 8, 2003
    #4
  5. "joeri" <> writes:

    > $var="(?i)(forty|fifty)";
    > $input = "Forty-ninth ST";
    > if ($input =~ /$var(\-ninth)\s[S|s]t/) {
    > print "$&\n";
    > }


    > Can I escape the ($i) or set the scope of it?


    /(?:$var)(\-ninth)\s[S|s]t/


    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Aug 8, 2003
    #5
  6. joeri wrote:
    >
    > I have an RE where I use the (?i) somewhere, but I want to undo that effect
    > further on in the RE.
    > Here's an example:
    >
    > $var="(?i)(forty|fifty)";


    Change that to:

    $var = '(?i:forty|fifty)';

    Which will limit the effects of case insensitivity to the enclosing
    parenthesis.


    > $input = "Forty-ninth ST";
    > if ($input =~ /$var(\-ninth)\s[S|s]t/) {

    ^^^^^
    Your character class is trying to match a 'S' or a '|' or a 's'. Do you
    really want to match a vertical bar here? The hyphen is not special in
    regular expressions, there is no need to backslash it.


    > print "$&\n";
    > }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Aug 8, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco Liedekerken

    Undo in TextBox

    Marco Liedekerken, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,365
    Marco Liedekerken
    Jun 26, 2003
  2. john farrow

    How do I undo WebRequest.RegisterPrefix

    john farrow, Apr 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    847
    john farrow
    Apr 13, 2004
  3. tshad

    Undo formatting

    tshad, Apr 6, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    1,088
    tshad
    Apr 13, 2005
  4. Morgan Bachu

    How to "undo" a database delete

    Morgan Bachu, Aug 30, 2005, in forum: ASP .Net
    Replies:
    13
    Views:
    10,628
    James Doughty
    Aug 31, 2005
  5. Andreas Wachhold

    Undo/Redo Java?

    Andreas Wachhold, May 4, 2004, in forum: Java
    Replies:
    2
    Views:
    4,939
    Mykola Rabchevskiy
    May 4, 2004
Loading...

Share This Page