Regular Expressions not working OK for me

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
ADougRay
Posts: 4
Joined: Fri Feb 11, 2005 12:27 pm
Location: Warwick, Rhode Island

Regular Expressions not working OK for me

Post by ADougRay »

Hi,

I have a file where I differentiate between strings and characters such that strings are delimited by double quotes and characters are delimited by single quotes. Not too tricky and kind of standard. For example "String" versus 'S'.

I wanted to change every occurance of '*'. to "*". and then change "." to '.'. I thought Regular Expressions would do this for me but, in both cases I got more than I bargained for. In the first '*' to "*", I got "*"String' instead of "String" (obviously I started with 'String') and in the second case, "." to '.', I got '.' instead of 'S'. I've worked around the problem - actually I used another editor I own that supports the same concepts - but I'd rather stick to TextPad if I could.

I am very willing to have someone tell me what I'm doing wrong, but I didn't find anything when I looked through old resources. Yes I DID cause the Regular Expression check mark to appear when doing the replace. I am using TextPad 4.7.3.

Thanks,
Doug Ray
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

These are regular expressions, not wildcards.

In a regular expression, * means any number of what precedes me.

The regular expression '*' consists of: '* followed by ' .
'* matches a (possibly empty) sequence of single quotes;
' matches a single quote.
So
'*' matches a (possibly empty) sequence of single quotes followed by a single quote; that is, a non-empty sequence of single quotes.

You need to search for '(.*)' and replace it with "\1",
and then to search for "(.)" and replace it with '\1'.
In a replacement expression, \1 means whatever matched the part of the regular expression contained in its first set of parentheses.

If there may be more than one quoted item on a line, you need to avoid matching everything from the first quote symbol to the last one. So you should search for '([^']*)' and "([^"])" .

The search expressions above assume you are using POSIX regular expression syntax:
Configuration | Preferences | Editor

[X] Use POSIX regular expression syntax
ADougRay
Posts: 4
Joined: Fri Feb 11, 2005 12:27 pm
Location: Warwick, Rhode Island

Thanks

Post by ADougRay »

That explains it. I do a bit of UNIX work but more BASh or KSh instead of POSIX. When you put it in those terms, it makes sense.

Thanks again,
Doug Ray
Post Reply