Page 1 of 1
How to search for this?
Posted: Mon Apr 26, 2010 11:03 am
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'.
Posted: Mon Apr 26, 2010 11:58 am
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.
Posted: Mon Apr 26, 2010 12:11 pm
by Bothkill
Works perfectly!
Thanks

Expanding on the original question...
Posted: Tue Apr 27, 2010 5:36 pm
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
Answering my own question
Posted: Tue Apr 27, 2010 7:24 pm
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."
Posted: Tue Apr 27, 2010 8:35 pm
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