in reply to Ucfirst on xml elements

This will uc the first letter following < and </:

$string =~ s!(</?)([^/>])!$1.uc($2)!eg;

The key is the /e modifier on the regexp, which indicates that the right-hand side of the substitution regexp is Perl code which needs to be evaluated, rather than just a plain string.

Breaking it down into its components:

s! (</?) # Match and capture '<' followed by optional '/' ([^/>]) # Match and capture the next char, as long as # it's not a '>' or '/'. ! $1 # Replace with '<' or '</'. . uc($2) # Concatenate with the uc of $2. !egx

This approach is error-prone though. What if the literal text (not found inside a tag) has a '<' character int it? You don't have to write your own parser, when CPAN has many already written.

Updated: Fixed issue with '</' case.


Dave

Replies are listed 'Best First'.
Re^2: Ucfirst on xml elements
by ambrus (Abbot) on Aug 29, 2004 at 17:57 UTC

    Let me note that

    s!(</?)([^/>])!$1\U$2!g;
    is a handy shortcut for
    s!(</?)([^/>])!$1.uc($2)!eg;
Re^2: Ucfirst on xml elements
by redhotpenguin (Deacon) on Aug 29, 2004 at 16:45 UTC
    That is wonderful, working great. Thank you. The data I am dealing with fortunately has no '<' characters in the literal text. And I have a couple of working parsers from CPAN implemented but for this simple task the regex is a better tool for the job I think.