Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #3475 > unrolled thread
| Started by | Jon Joseph <josco.jon@gmail.com> |
|---|---|
| First post | 2011-07-04 10:47 +0000 |
| Last post | 2011-07-05 09:10 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Operations on RegularExpression matches Jon Joseph <josco.jon@gmail.com> - 2011-07-04 10:47 +0000
Re: Operations on RegularExpression matches "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-07-05 09:15 +0000
Re: Operations on RegularExpression matches Albert Retey <awnl@gmx-topmail.de> - 2011-07-05 09:10 +0000
| From | Jon Joseph <josco.jon@gmail.com> |
|---|---|
| Date | 2011-07-04 10:47 +0000 |
| Subject | Operations on RegularExpression matches |
| Message-ID | <ius5nq$2g4$1@smc.vnet.net> |
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.
[toc] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-07-05 09:15 +0000 |
| Message-ID | <iuukn4$eq6$1@smc.vnet.net> |
| In reply to | #3475 |
Try the following: StringReplace[string, RegularExpression["(^.)"] :> ToUpperCase["$1"]] On Mon, 04 Jul 2011 11:47:22 +0100, Jon Joseph <josco.jon@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Albert Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-07-05 09:10 +0000 |
| Message-ID | <iuukdh$eln$1@smc.vnet.net> |
| In reply to | #3475 |
Hi, > > 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? the problem is that ToUpperCase is done on the pattern, not the match. You need RuleDelayed so that ToUpperCase is done on the matching string: StringReplace[string, RegularExpression["(^.)"] :> ToUpperCase["$1"]] this is not necessary with the StringExpression approach since then ToUpperCase[x] remains unevaluated since x is no string. hth, albert
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web