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'.
How to search for this?
Moderators: AmigoJack, bbadmin, helios, MudGuard
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
-
slohcinbocaj
- Posts: 2
- Joined: Tue Apr 27, 2010 5:06 pm
Expanding on the original question...
...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
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
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."
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
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
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