Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

RegEx Problem

by L0rdPhi1 (Sexton)
on May 29, 2002 at 00:33 UTC ( [id://169944]=perlquestion: print w/replies, xml ) Need Help??

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

I've been trying to get strings in the format of "cowsCanFly" to change to "cows-can-fly" using RegEx and haven't really got anywhere. The original string will always be in the format "sheepAreVeryCool" and "iLikeCows" (first letter of first word lowercase and every other word starts with a capital letter) while what I need it to generate always needs to be in the format "sheep-are-very-cool" and "i-like-cows" (all lowercase, words separated with hyphens). I bet someone out there could figure this out pretty fast... so I thought I'd give it a shot and ask :)

Replies are listed 'Best First'.
Re: RegEx Problem
by Kanji (Parson) on May 29, 2002 at 00:38 UTC
    s/\B([A-Z])(?=[a-z])/-\l$1/g; # ?

    Although if you can guarantee that a capital letter will never occur at the beginning of the string and will always denote a new word (ie, samIAm), you can simplify that to just...

    s/([A-Z])/-\l$1/g;

        --k.


      I'm currently using:
      $moo = 'boo-hoo-hoo'; $hoo = lcfirst join '', map { ucfirst $_ } split /-/, $moo;
      To do that exact reverse of the above... is there a better way (I'm sure there is)?

        Ah, I should be able to redeem myself on this one...

        $str =~ s/-([a-z])/uc $1/ge;

        /me crosses fingers

        Update: Oh dang. Damn you, Kanji ;)

Re: RegEx Problem
by clintp (Curate) on May 29, 2002 at 00:39 UTC
    s/([A-Z])/-lc($1)/eg
    Near as I can tell.
      s/([A-Z])/-lc($1)/eg

      Near as I can tell.

      -i think it loses on an edge condition.
Re: RegEx Problem
by dws (Chancellor) on May 29, 2002 at 06:21 UTC
    Here's one I hadn't tried before -- /e nested within /e.
    $phrase = "NoMatch cowsCanFly sheepAreVeryCool NoMatch"; $phrase =~ s{ \b ( [a-z]+ (?:[A-Z][a-z]+)+ ) \b }{ my $word = $1; $word =~ s/([A-Z])/"-" . lc($1)/eg; $word; }gex; print $phrase, "\n";
      If that works it's only because you got lucky. In general the regex engine is not reentrant. Seg-faults are the most common result of this kind of code...

      -sam

        Nope, the regex is fine as it isn't re-entrant. If there was regex *within* the regex then that would be re-entrant, but as far as perl is concerned you can do as you please in the replace part of a s///.

        So this code will break[1]

        my $str = "foo bar baz quux"; $str =~ s<([a-z]+) (?{ s| \1|\L$&|; })><\U$1>xg;
        But this is fine
        $str =~ s<([a-z]+ ?)><local $_ = $1; s/$1/\U$&/g; $_>eg;

        HTH

        _________
        broquaint

        [1] in theory, I'll be happy to change it to a proper re-entrant regex if someone will provide me with one

Re: RegEx Problem
by tea-ice (Initiate) on May 29, 2002 at 09:36 UTC
    Hi guys, new to the boards, and yes this is my first post. LP try this. I have tested it and should work for you as well.
    $word=<STDIN>; chomp ($word); $word=~s/[A-Z]/-$&/g; #converts A to -A $word=~tr [A-Z][a-z]; #converts -A to -a print $word
Re: RegEx Problem
by lestrrat (Deacon) on May 29, 2002 at 00:40 UTC

    Here's a sloppy example....

    my $str = "cowsCanFly"; $str =~ s/\G([A-Z]?[a-z])/lcfirst($1) . '-'/eg; chop($str); print $str, "\n";

    update: man, I feel ashamed posting this. Kanji++

Re: RegEx Problem
by marvell (Pilgrim) on May 29, 2002 at 11:00 UTC
    $word =~ s/[A-Z]/-\l$&/g;

    --
    Steve Marvell

      Oh, sorry for not responding sooner :)

      Kanji's coded worked fine both times :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found