|
|
| nonsequitur 2005-04-08, 5:52 pm |
| I want to use regexp to match a string, say "ABC", that is either
preceeded by "X" or is at the beginning of the text string. How can I
create a regexp that conditionally checks for X or ^ (line anchor)?
| |
| shakahshakah@gmail.com 2005-04-08, 5:52 pm |
| nonsequitur wrote:
> I want to use regexp to match a string, say "ABC", that is either
> preceeded by "X" or is at the beginning of the text string. How can
I
> create a regexp that conditionally checks for X or ^ (line anchor)?
How about '(^|.*X)ABC' ?
jc@soyuz:/tmp$ cat test.input
ABC (should match)
zzz ABC (should not match)
yyy XABC (should match)
XABC (should match)
WABC (should not match)
jc@soyuz:/tmp$ egrep '(^|.*X)ABC' test.input
ABC (should match)
yyy XABC (should match)
XABC (should match)
| |
| Måns Rullgård 2005-04-08, 5:52 pm |
| "shakahshakah@gmail.com" <shakahshakah@gmail.com> writes:
> nonsequitur wrote:
> I
>
> How about '(^|.*X)ABC' ?
That will work with some regexp implementations, but not all. In
some variants, ^ and $ lose their special meanings inside parens.
Also, don't forget that some variants (e.g. emacs) use \(\) as
subpatter delimiters.
--
Måns Rullgård
mru@inprovide.com
| |
| shakahshakah@gmail.com 2005-04-08, 8:48 pm |
|
M=E5ns Rullg=E5rd wrote:
> "shakahshakah@gmail.com" <shakahshakah@gmail.com> writes:
>
can[vbcol=seagreen]
anchor)?[vbcol=seagreen]
>
> That will work with some regexp implementations, but not all. In
> some variants, ^ and $ lose their special meanings inside parens.
> Also, don't forget that some variants (e.g. emacs) use \(\) as
> subpatter delimiters.
>
> --
> M=E5ns Rullg=E5rd
> mru@inprovide.com
You can always do '^(ABC|.*XABC)' then (though the subpattern
delimiters issues is still there, I guess).
| |
| Måns Rullgård 2005-04-08, 8:48 pm |
| "shakahshakah@gmail.com" <shakahshakah@gmail.com> writes:
> Måns Rullgård wrote:
>
> You can always do '^(ABC|.*XABC)' then (though the subpattern
> delimiters issues is still there, I guess).
^(|.*X)ABC is less repetitive.
--
Måns Rullgård
mru@inprovide.com
| |
| shakahshakah@gmail.com 2005-04-08, 8:48 pm |
|
M=E5ns Rullg=E5rd wrote:
> "shakahshakah@gmail.com" <shakahshakah@gmail.com> writes:
>
for[vbcol=seagreen]
>
> ^(|.*X)ABC is less repetitive.
>
> --
> M=E5ns Rullg=E5rd
> mru@inprovide.com
Interesting, I didn't know you could have an "empty" portion of a
parenthesized subexpression. You learn something new every day...
| |
| T.M. Sommers 2005-04-13, 6:00 pm |
| nonsequitur wrote:
> I want to use regexp to match a string, say "ABC", that is either
> preceeded by "X" or is at the beginning of the text string. How can I
> create a regexp that conditionally checks for X or ^ (line anchor)?
Try:
^X?ABC
--
Thomas M. Sommers -- tms@nj.net -- AB2SB
| |
| Matthias Buelow 2005-04-13, 6:00 pm |
| "T.M. Sommers" <tms@nj.net> writes:
> nonsequitur wrote:
>
> Try:
>
> ^X?ABC
Counter-example: "abcXABC".
The OP could try ^(|.*X)ABC if he's using extended regular expressions
(such as used by egrep(1)).
mkb.
| |
| Måns Rullgård 2005-04-13, 6:00 pm |
| Matthias Buelow <mkb@incubus.de> writes:
> "T.M. Sommers" <tms@nj.net> writes:
>
>
> Counter-example: "abcXABC".
>
> The OP could try ^(|.*X)ABC if he's using extended regular expressions
> (such as used by egrep(1)).
^(.*X)?ABC is another variation.
--
Måns Rullgård
mru@inprovide.com
| |
| T.M. Sommers 2005-04-13, 8:52 pm |
| Matthias Buelow wrote:
> "T.M. Sommers" <tms@nj.net> writes:
>
>
> Counter-example: "abcXABC".
>
> The OP could try ^(|.*X)ABC if he's using extended regular expressions
> (such as used by egrep(1)).
I read it to mean that the X was alone at the beginning of the
line. Rereading indicates I was wrong in that understanding.
--
Thomas M. Sommers -- tms@nj.net -- AB2SB
| |
| Moshe Jacobson 2005-04-14, 7:51 am |
| T.M. Sommers <tms@nj.net> wrote:
> nonsequitur wrote:
[vbcol=seagreen]
> Try:
> ^X?ABC
That will not match "fooXABC", which the OP specified should match.
Moshe
--
*** SPAM BLOCK: Remove bra before replying! ***
http://runslinux.net :: moshe at runslinux dot net :: AIM: Jehsom
|
|
|
|