escape a whole regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tassos

    escape a whole regex

    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/;

  • Gunnar Hjalmarsson

    #2
    Re: escape a whole regex

    Tassos wrote:[color=blue]
    > 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).[/color]

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

    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

    Comment

    • Jim Gibson

      #3
      Re: escape a whole regex

      In article <1071247180.256 610@athprx02>, Tassos <chatasos@yahoo .com>
      wrote:
      [color=blue]
      > 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/;
      >[/color]

      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.

      Comment

      Working...