regex line anchor
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > regex line anchor




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    regex line anchor  
nonsequitur


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-08-05 10: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)?






[ Post a follow-up to this message ]



    Re: regex line anchor  
shakahshakah@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-08-05 10: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)






[ Post a follow-up to this message ]



    Re: regex line anchor  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-08-05 10: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





[ Post a follow-up to this message ]



    Re: regex line anchor  
shakahshakah@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-09-05 01:48 AM


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).






[ Post a follow-up to this message ]



    Re: regex line anchor  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-09-05 01:48 AM

"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





[ Post a follow-up to this message ]



    Re: regex line anchor  
shakahshakah@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-09-05 01:48 AM


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...






[ Post a follow-up to this message ]



    Re: regex line anchor  
T.M. Sommers


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-13-05 11: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






[ Post a follow-up to this message ]



    Re: regex line anchor  
Matthias Buelow


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-13-05 11: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.





[ Post a follow-up to this message ]



    Re: regex line anchor  
Måns Rullgård


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-13-05 11: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





[ Post a follow-up to this message ]



    Re: regex line anchor  
T.M. Sommers


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-14-05 01:52 AM

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






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:38 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register