How to search for this?

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
Bothkill
Posts: 2
Joined: Mon Apr 26, 2010 10:58 am

How to search for this?

Post by Bothkill »

How to search for this pattern:

a specific character + any number of characters + another specific character

E.g.

Suppose there is

abcc

and I want to find a sequence that begins with 'a' and ends with the first 'c'.

I tried with ''a.*c' but the search selects the hole sequence 'abcc' whereas I only need 'abc'.
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

With a modern regex recogniser you might use non-greedy repetition:
a.*?c
but TextPad hasn't got a modern regex recogniser. However, you can use
a[^c]*c
instead. This matches an a, followed by a sequence of characters that aren't c, followed by a c.
Bothkill
Posts: 2
Joined: Mon Apr 26, 2010 10:58 am

Post by Bothkill »

Works perfectly!

Thanks :D
slohcinbocaj
Posts: 2
Joined: Tue Apr 27, 2010 5:06 pm

Expanding on the original question...

Post by slohcinbocaj »

...what if I then want to replace the leading\trailing characters in the search string but keep everything else the same? "abcc" changes to "~b~c" (replaced leading & trailing search char with "~".

Specifically, I have a vcard (vcf) file that I am trying to reformat the phone number seperators for. So my search is similar in flavor to the original question posted in this thread. I am searching for "-[0-9][0-9][0-9]-" (the search seems to work just fine) but I want to replace the dashes with periods but keep the numbers in between the same, resulting in ".[0-9][0-9][0-9].". So for an input of 800-555-1234, I desire an output of 800.555.1234
slohcinbocaj
Posts: 2
Joined: Tue Apr 27, 2010 5:06 pm

Answering my own question

Post by slohcinbocaj »

I am new to regex and finally pieced together enough syntax info from this forum and online to get something that worked.

Search string is "-([0-9][0-9][0-9])-"
Replace string is ".\1."
ben_josephs
Posts: 2464
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Well done!

Your regex shows that you're using Posix syntax. This is a Good Thing. It will (usually) reduce the number of backslashes in your regexes and make them more like those recognised by most modern regex tools.

Here's a slightly shorter version:
-([0-9]{3})-

Or you might search for
([0-9]{3})-([0-9]{3})-([0-9]{4})
and replace it with
\1.\2.\3
Post Reply