another problem with look-around

E

eric.hall

Sorry for the newbie questions

I need a one-line regexp that only return a match if the test string
only exists once in the input. For example, find 'abc' if no other
occurrences of 'abc' exist anywhere in the input string of 'abc abc
abc'. This should be simple, but I'm having a lot of trouble with
limiting the scope.

/(?!(abc)).*abc.*/ doesn't work, because that traps the last instance
(which is not followed by anything). /.*abc.*(?<!(abc))/ has the same
problem in reverse. Adding them together for
/(?!(abc)).*abc.*(?<!(abc))/ traps 'bc abc ab' in the middle, which is
not what I want (I need to return a match only if it exists in
isolation).

This gets even harder if the input has lots of other noise in the
input. For example, 'abc def abc def abc'. Suffice it to say that my
input has lots of junk, including non-word characters.

I thought that something like this would be a fairly stock regexp but I
haven't been able to locate anything in google that seems relevant.
Surely this is possible?

Thanks for the help!
 
B

Brian McCauley

I need a one-line regexp that only return a match if the test string
only exists once in the input. For example, find 'abc' if no other
occurrences of 'abc' exist anywhere in the input string of 'abc abc
abc'. This should be simple, but I'm having a lot of trouble with
limiting the scope.

Would it be sufficient to look for 'abc' but not if the string contains
abc at least twice?

/abc/ && !/abc.*abc/

Or if you insist on a single regex:

/^(?!abc.*abc).*abc/
 
M

Michael Abootorab

try this

if [[ "$x" = *abc* ]];then
x=${x%?abc*}
if [[ "$x" != *abc* ]];then
echo its true
fi
fi

thanks
Michael
 
K

Keith Keller

if [[ "$x" = *abc* ]];then
x=${x%?abc*}
if [[ "$x" != *abc* ]];then
echo its true
fi
fi

That's neither perl (this is comp.lang.perl.misc!) nor one-line (which
is what the OP asked for). See Brian's post for more appropriate
suggestions. (Obviously, the above could be easily written in Perl, if
the OP relaxes his one-line requirement.)

--keith
 
E

eric.hall

/^(?!abc.*abc).*abc/

That works in my specific case, so thanks. It doesn't work in the
generalized case, since it requires the leading characters to match.
 
B

Brian McCauley

That works in my specific case, so thanks. It doesn't work in the
generalized case, since it requires the leading characters to match.

Opps, sorry, typo:

/^(?!.*abc.*abc).*abc/
 
C

Charles DeRykus

Sorry for the newbie questions

I need a one-line regexp that only return a match if the test string
only exists once in the input. For example, find 'abc' if no other
occurrences of 'abc' exist anywhere in the input string of 'abc abc
abc'. This should be simple, but I'm having a lot of trouble with
limiting the scope.

/(?!(abc)).*abc.*/ doesn't work, because that traps the last instance
(which is not followed by anything). /.*abc.*(?<!(abc))/ has the same
problem in reverse. Adding them together for
/(?!(abc)).*abc.*(?<!(abc))/ traps 'bc abc ab' in the middle, which is
not what I want (I need to return a match only if it exists in
isolation).

This gets even harder if the input has lots of other noise in the
input. For example, 'abc def abc def abc'. Suffice it to say that my
input has lots of junk, including non-word characters.

I thought that something like this would be a fairly stock regexp but I
haven't been able to locate anything in google that seems relevant.

Can't you simply use:

$count++ while $input =~ /$test_string/g;
print $count==1 ? "once" : "nope";

Or, is this strictly a one-liner... If so, I suspect the
one-liner will be slower.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top