Re: Operations on RegularExpression matches
- To: mathgroup at smc.vnet.net
- Subject: [mg120015] Re: Operations on RegularExpression matches
- From: Heike Gramberg <heike.gramberg at gmail.com>
- Date: Tue, 5 Jul 2011 05:11:54 -0400 (EDT)
- References: <201107041044.GAA02505@smc.vnet.net>
Try using RuleDelayed: StringReplace[string, RegularExpression["(^.)"] :> ToUpperCase["$1"]] By using RuleDelayed (:>) instead of Rule (->), the right-hand side of the replacement rule isn't evaluated until after the rule is used. Heike On 4 Jul 2011, at 11:44, Jon Joseph wrote: > All: > > I have string of all lower case letters: > > string = "this is a string" > > I wish to capitalize the first letter: > > StringReplace[string, StartOfString ~~ x_ -> ToUpperCase[x]] (* Does want I want *) > > Now I try to do the same thing using RegularExpression: > > StringReplace[string, RegularExpression["(^.)"] -> ToUpperCase["$1"]] > > which fails (returns original string without capitalization). > > I know the match is occurring since > > StringReplace[string, RegularExpression["(^.)"] -> ToUpperCase["$1X"]] > > returns > > "tXhis is a string" > > What am I missing about using the matched part of a regular expression? > > Thanks. >
- References:
- Operations on RegularExpression matches
- From: Jon Joseph <josco.jon@gmail.com>
- Operations on RegularExpression matches