in reply to Extracting names from string

($reversed_name) = $string =~ m!/(.*?)\|!;

The perlre and perlop help files give more details on regular expressions.

Update: Escaped the |.

Replies are listed 'Best First'.
Re^2: Extracting names from string
by Random_Walk (Prior) on Feb 08, 2005 at 16:30 UTC
    Surely
    echo "m!/(.*)|!;" | perl -pe's/\|/\\\|/'
    ?

    Update

    OK, by the time I posted this you already had. I only took so long to post it 'cos I found it hard to believe ikegami could make mistakes :)

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re^2: Extracting names from string
by Anonymous Monk on Feb 08, 2005 at 16:10 UTC
    What does "m!/(.*?)|!;" actually do? Arnt you missing the ending "/" Basically what I need is for it to grab all data within these characters starting with "/" and ending with "|". Im not sure if I can extract the data using reg expression. Thanks again, Jack

      /.../, m/.../, m#...#, m!...! and so on are all equivalent. The character following the m will be used to delimit the regexp.

      Use
      ($reversed_name) = $string =~ m/\/(.*?)\|/;
      if you prefer. It means the same thing, but it's not quite as readable.

      foreach $string ( "s005219/Doe John|John.Doe\\", "s0052194/Doe Thomas|Thomas.Doe\\", ) { ($reversed_name) = $string =~ m!/(.*?)\|!; print($reversed_name, "\n"); } # Prints # ====== # Doe John # Doe Thomas

      Alternatively,

      foreach $string ( "s005219/Doe John|John.Doe\\", "s0052194/Doe Thomas|Thomas.Doe\\", ) { if (m!/(.*?)\|!) { print("$1\n"); } }