Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

quick regex question

by Grygonos (Chaplain)
on Jul 15, 2003 at 13:49 UTC ( [id://274408]=perlquestion: print w/replies, xml ) Need Help??

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

$row[0] =~ s/\//\s/; should this not substitute all /'s with null?
ie..12/31/2002 becomes 12312002

Replies are listed 'Best First'.
Re: quick regex question
by Limbic~Region (Chancellor) on Jul 15, 2003 at 13:53 UTC
    Grygonos,
    If your goal is to delete a specific character and you want to do it through your entire string - it would probably be better to use tr.
    $row[0] =~ tr /\///d;
    To answer the question you asked - you would need to add the g modifier to make the substitution global and to make it a null, you would make the subsitution empty:
    $row[0] =~ s/\///g;
    You may also want to check out changing your delimiters so that you avoid the "leaning toothpick" syndrome.

    Cheers - L~R

Re: quick regex question
by dreadpiratepeter (Priest) on Jul 15, 2003 at 13:55 UTC
    No 12s31/2002 because:
  • The subtitution part of the regexp is treated as a double quoted string and \s is not a valid escape (enabling warnings would have told you that)
  • Only the first occurance is replaced unless you specify the /g modifier.
    You want  s/\///g;
    Unless you wanted a literal null character in which case its \0 in the substituion (I think I don't have my camel with me).

    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      in response to the responses..

      • thanks . I meant to post the one without the /s hehe I had taken it out and grabbed the wrong one to paste. thanks . I forgot about the g mod
      • tr does seem like a better option.
      • I don't choose the delimiter unfortunately :( unless you are referring to the / in the regex... which I didnn't know there was any way to use another one? did you mean $row[0] =~ s,\/,,glike using a different regex delimiter? or did you mean the /'s in the field?


      • Thanks for all your responses .. problem solved... multiple ways :)
        Yeah, you can use (just about) anything as your regexp delimiter, and if you use something other than / you don't need to escape it. So s,/,,g or tr,/,,d (or whatever other delimiter strikes your fancy; I'm fond of matched delimiters like tr{/}{}d, but that's just me).
Re: quick regex question
by sschneid (Deacon) on Jul 15, 2003 at 13:52 UTC
    \s is whitespace. try s/\///g;.

    -s.
      \s is only whitespace in a regexp, not in the substitution part. There \s is just an s.

      Abigail

Re: quick regex question
by arthas (Hermit) on Jul 15, 2003 at 14:35 UTC

    If you want to substitue all '/' with nulls (charchters with ASCII code 0) you should do:

    $row[0] =~ s/\//\0/g;

    But you probably just want to remove '/', so it's:

    $row[0] =~ s/\///g;

    Hope this helps!

    Michele.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://274408]
Approved by MrCromeDome
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-24 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found