Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Need another method to achieve the task.

Expand Messages
  • Jeff 'japhy' Pinyan
    ... (?:...) is like (...) in that it groups a bunch of regex pieces together as a unit, so you can say /a(?:bc)+/ and it ll match abc , abcbc , abcbcbc ,
    Message 1 of 5 , Dec 2, 2005
    • 0 Attachment
      On Dec 1, Sreeram B S said:

      >> $string =~ s{
      >>
      >> ( # capture to $1 {
      >> (?: ^ | _ ) # either the beginning of the string or an _
      >> [^a-zA-Z]* # and then zero or more non-letters
      >> ) # }
      >>
      >> ( # capture to $2 {
      >> [a-zA-Z] # one letter
      >> ) # }
      >>
      >> }{$1\u$2}xg; # and replace with $1 followed by uppercased $2
      >
      > Thanks for the detailed explaination of both the
      > methods. However, I failed to clealy understand the
      > first portion of the regex ie ((?:^|_)[^a-zA-Z]*)
      > What is the role of (?:^|_) here?

      (?:...) is like (...) in that it groups a bunch of regex pieces together
      as a unit, so you can say /a(?:bc)+/ and it'll match "abc", "abcbc",
      "abcbcbc", etc. The difference between (?:...) and (...) is that (?:...)
      doesn't create a capturing variable ($1, $2, $3, etc.) like (...) does.

      In my regex, (?:^|_) is being used to specify the left-hand boundary of
      the thing we're trying to uppercase. In the string "this_that_those", I
      want to deal with the first letter after each '_', but I ALSO want to deal
      with the very first letter I see, because the string doesn't BEGIN with an
      underscore character. If the string were "_this_that_those", then my
      regex would just be

      s/(_[^a-zA-Z]*)([a-zA-Z])/$1\u$2/g;

      --
      Jeff "japhy" Pinyan % How can we ever be the sold short or
      RPI Acacia Brother #734 % the cheated, we who for every service
      http://www.perlmonks.org/ % have long ago been overpaid?
      http://princeton.pm.org/ % -- Meister Eckhart
    Your message has been successfully submitted and would be delivered to recipients shortly.