Retaining capitalization in regex

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
BrianR
Posts: 3
Joined: Fri Jun 24, 2011 2:11 pm

Retaining capitalization in regex

Post by BrianR »

Hello,
Struggling with this: I need to reformat multiple instances of similar tags, from:

<script>document.write(FOOBAR)</script>foobar
or:
<script>document.write(BARGLE)</script>Bargle

to:

<a href="event:Foobar">foobar</a>
and:
<a href="event:Bargle">Bargle</a>

However I can't seem to get any regex syntax that'll retain the occasional initial capitalization.

I had hoped that [:alnum:] would automatically retain the capitals, but no.

So I've tried ([[:alnum:]]*), ([a-z0-9]*) and ([A-Za-z0-9]*), no success, either with match case on or off.

Tried ([A-Z]?[a-z0-9]*), hoping the 'zero or one' operator would pick up the initial caps and default to the lowercase string where none is present. When I do this with Match Case off, it mirrors the previous behaviour, with Match Case on it retains the initial capital, but leaves the foobar/Bargle string *outside* the closing </a> tag.

I'm sure there's something really quite basic I'm missing here, or is it simply that there's no way to retain lettercase in this manner?
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

I don't fully understand: you haven't told us the exact regular expressions and replacement expressions you've tried.

Try this:

Use "Posix" regular expression syntax:
Configure | Preferences | Editor

[X] Use POSIX regular expression syntax
Find what: <script>document.write\(([a-z0-9]*)\)</script>([a-z0-9]*)
Replace with: <a href="event:\1">\2</a>

[X] Regular expression

Replace All
BrianR
Posts: 3
Joined: Fri Jun 24, 2011 2:11 pm

Post by BrianR »

Sorry, initial attempts were along the lines of

Find:

Code: Select all

<script>document.write\(([A-Z])([A-Z0-9]*)\)</script>([a-zA-Z0-9]*)
Replace:

Code: Select all

<a href="event:\u\1\L\2">\3</a>
Using POSIX syntax, as you noted.
BrianR
Posts: 3
Joined: Fri Jun 24, 2011 2:11 pm

Post by BrianR »

As a followup, I'm now suspicious that the culprit is the \L I've been using to reformat the FOOBAR string inside the parentheses, as using your code example it preserves the initial capitals in the expression tagged \2 (in your example), but of course doesn't fix the capitalization of the \1 tag.

edit: And that is indeed what was happening. Fixed by adding the \E 'turn off the lowercasing' after \2 in my code example above.
Post Reply