escape a whole regex

Discussion in 'Perl' started by Tassos, Dec 12, 2003.

  1. Tassos

    Tassos Guest

    I have a file with 2 fields (";" is the seperator): 1 string (field1) & 1 regex (field2)

    test1;([\w\-\.:/]+)
    test2;(\w\d [\d\/]+)
    test3;([\w\d\.])

    I want while reading the file to have an array being filled with field1 kai another array
    being filled with field2 (easy until here). But field2 should be escaped (all possible
    metacharacters) because it will be used as a regex.

    e.x.
    my $temp =~ s/$field1/$field2/;
     
    Tassos, Dec 12, 2003
    #1
    1. Advertising

  2. Tassos wrote:
    > I have a file with 2 fields (";" is the seperator): 1 string
    > (field1) & 1 regex (field2)
    >
    > test1;([\w\-\.:/]+)
    > test2;(\w\d [\d\/]+)
    > test3;([\w\d\.])
    >
    > I want while reading the file to have an array being filled with
    > field1 kai another array being filled with field2 (easy until
    > here).


    Good. :)

    > But field2 should be escaped (all possible metacharacters) because
    > it will be used as a regex.
    >
    > e.x.
    > my $temp =~ s/$field1/$field2/;


    What you are saying here does not make sense. Please show us the code
    you have written so far, illustrating the problem you encounter.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 12, 2003
    #2
    1. Advertising

  3. Tassos

    Jim Gibson Guest

    In article <1071247180.256610@athprx02>, Tassos <>
    wrote:

    > I have a file with 2 fields (";" is the seperator): 1 string (field1) & 1
    > regex (field2)
    >
    > test1;([\w\-\.:/]+)
    > test2;(\w\d [\d\/]+)
    > test3;([\w\d\.])
    >
    > I want while reading the file to have an array being filled with field1 kai
    > another array
    > being filled with field2 (easy until here). But field2 should be escaped (all
    > possible
    > metacharacters) because it will be used as a regex.
    >
    > e.x.
    > my $temp =~ s/$field1/$field2/;
    >


    This newsgroup is defunct. Please do not start threads here. Try
    comp.lang.perl.misc in the future for better response.

    It is best to attempt to write a program to do what you are trying and
    then post it if it doesn't work. Check out the quote regex operator
    qr//. It should help you do what you want.
     
    Jim Gibson, Dec 12, 2003
    #3
    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. \A_Michigan_User\
    Replies:
    2
    Views:
    937
    \A_Michigan_User\
    Aug 21, 2006
  2. slomo
    Replies:
    5
    Views:
    1,625
    Duncan Booth
    Dec 2, 2007
  3. Replies:
    3
    Views:
    822
    Reedick, Andrew
    Jul 1, 2008
  4. Replies:
    8
    Views:
    2,904
    Lie Ryan
    Jun 14, 2009
  5. Jon Garvin

    regex to escape special characters

    Jon Garvin, Feb 10, 2009, in forum: Ruby
    Replies:
    4
    Views:
    172
    Tom Cloyd
    Feb 11, 2009
Loading...

Share This Page