Regular expression for a string containing one word but not another
up vote
69
down vote
favorite
I'm setting up some goals in Google Analytics and could use a little regex help.
Lets say I have 4 URLs
http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1
I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm
I know that to find a string that does NOT contain another string I can use this expression:
(^((?!details.cfm).)*$)
But, I'm not sure how to add in the selector=size portion.
Any help would be greatly appreciated!
regex google-analytics regex-negation
add a comment |
up vote
69
down vote
favorite
I'm setting up some goals in Google Analytics and could use a little regex help.
Lets say I have 4 URLs
http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1
I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm
I know that to find a string that does NOT contain another string I can use this expression:
(^((?!details.cfm).)*$)
But, I'm not sure how to add in the selector=size portion.
Any help would be greatly appreciated!
regex google-analytics regex-negation
add a comment |
up vote
69
down vote
favorite
up vote
69
down vote
favorite
I'm setting up some goals in Google Analytics and could use a little regex help.
Lets say I have 4 URLs
http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1
I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm
I know that to find a string that does NOT contain another string I can use this expression:
(^((?!details.cfm).)*$)
But, I'm not sure how to add in the selector=size portion.
Any help would be greatly appreciated!
regex google-analytics regex-negation
I'm setting up some goals in Google Analytics and could use a little regex help.
Lets say I have 4 URLs
http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1
I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm
I know that to find a string that does NOT contain another string I can use this expression:
(^((?!details.cfm).)*$)
But, I'm not sure how to add in the selector=size portion.
Any help would be greatly appreciated!
regex google-analytics regex-negation
regex google-analytics regex-negation
asked Jun 1 '10 at 20:21
Chris Stahl
5241615
5241615
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
101
down vote
accepted
This should do it:
^(?!.*details.cfm).*selector=size.*$
^.*selector=size.*$
should be clear enough. The first bit, (?!.*details.cfm)
is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).
4
FYI, check out regexr.com for a nice way to test these expressions out.
– Joshua Pinter
Apr 8 '14 at 14:23
Brilliant, this helped. Good explanation
– user219628
Dec 21 '15 at 18:02
Always forget about negative lookahead and it's so useful
– Alexei Blue
Feb 20 at 15:35
add a comment |
up vote
5
down vote
regex could be (perl syntax):
`/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`
add a comment |
up vote
1
down vote
^(?=.*selector=size)(?:(?!details.cfm).)+$
If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:
^[^?]*+(?<!details.cfm).*?selector=size.*$
This assumesselector=size
is always beforedetails.cfm
, which isn't the case in the last url.
– Kobi
Jun 1 '10 at 20:34
Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
– Kobi
Jun 1 '10 at 20:47
@Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
– Tomalak
Jun 1 '10 at 20:48
add a comment |
up vote
0
down vote
I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).
My original command:
tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '
Now becomes (with "-P" perl switch):
tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'
add a comment |
up vote
-4
down vote
Simple way to do this is to specify 0 instances of the string by doing the following
(string_to_exclude){0}
2
This does not work.
– Austin Henley
Oct 10 '12 at 20:06
this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
– Zoey Hewll
Apr 4 '17 at 5:01
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
101
down vote
accepted
This should do it:
^(?!.*details.cfm).*selector=size.*$
^.*selector=size.*$
should be clear enough. The first bit, (?!.*details.cfm)
is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).
4
FYI, check out regexr.com for a nice way to test these expressions out.
– Joshua Pinter
Apr 8 '14 at 14:23
Brilliant, this helped. Good explanation
– user219628
Dec 21 '15 at 18:02
Always forget about negative lookahead and it's so useful
– Alexei Blue
Feb 20 at 15:35
add a comment |
up vote
101
down vote
accepted
This should do it:
^(?!.*details.cfm).*selector=size.*$
^.*selector=size.*$
should be clear enough. The first bit, (?!.*details.cfm)
is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).
4
FYI, check out regexr.com for a nice way to test these expressions out.
– Joshua Pinter
Apr 8 '14 at 14:23
Brilliant, this helped. Good explanation
– user219628
Dec 21 '15 at 18:02
Always forget about negative lookahead and it's so useful
– Alexei Blue
Feb 20 at 15:35
add a comment |
up vote
101
down vote
accepted
up vote
101
down vote
accepted
This should do it:
^(?!.*details.cfm).*selector=size.*$
^.*selector=size.*$
should be clear enough. The first bit, (?!.*details.cfm)
is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).
This should do it:
^(?!.*details.cfm).*selector=size.*$
^.*selector=size.*$
should be clear enough. The first bit, (?!.*details.cfm)
is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).
answered Jun 1 '10 at 20:26
Kobi
106k33217263
106k33217263
4
FYI, check out regexr.com for a nice way to test these expressions out.
– Joshua Pinter
Apr 8 '14 at 14:23
Brilliant, this helped. Good explanation
– user219628
Dec 21 '15 at 18:02
Always forget about negative lookahead and it's so useful
– Alexei Blue
Feb 20 at 15:35
add a comment |
4
FYI, check out regexr.com for a nice way to test these expressions out.
– Joshua Pinter
Apr 8 '14 at 14:23
Brilliant, this helped. Good explanation
– user219628
Dec 21 '15 at 18:02
Always forget about negative lookahead and it's so useful
– Alexei Blue
Feb 20 at 15:35
4
4
FYI, check out regexr.com for a nice way to test these expressions out.
– Joshua Pinter
Apr 8 '14 at 14:23
FYI, check out regexr.com for a nice way to test these expressions out.
– Joshua Pinter
Apr 8 '14 at 14:23
Brilliant, this helped. Good explanation
– user219628
Dec 21 '15 at 18:02
Brilliant, this helped. Good explanation
– user219628
Dec 21 '15 at 18:02
Always forget about negative lookahead and it's so useful
– Alexei Blue
Feb 20 at 15:35
Always forget about negative lookahead and it's so useful
– Alexei Blue
Feb 20 at 15:35
add a comment |
up vote
5
down vote
regex could be (perl syntax):
`/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`
add a comment |
up vote
5
down vote
regex could be (perl syntax):
`/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`
add a comment |
up vote
5
down vote
up vote
5
down vote
regex could be (perl syntax):
`/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`
regex could be (perl syntax):
`/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`
edited Jun 1 '10 at 20:37
answered Jun 1 '10 at 20:35
djipko
913
913
add a comment |
add a comment |
up vote
1
down vote
^(?=.*selector=size)(?:(?!details.cfm).)+$
If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:
^[^?]*+(?<!details.cfm).*?selector=size.*$
This assumesselector=size
is always beforedetails.cfm
, which isn't the case in the last url.
– Kobi
Jun 1 '10 at 20:34
Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
– Kobi
Jun 1 '10 at 20:47
@Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
– Tomalak
Jun 1 '10 at 20:48
add a comment |
up vote
1
down vote
^(?=.*selector=size)(?:(?!details.cfm).)+$
If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:
^[^?]*+(?<!details.cfm).*?selector=size.*$
This assumesselector=size
is always beforedetails.cfm
, which isn't the case in the last url.
– Kobi
Jun 1 '10 at 20:34
Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
– Kobi
Jun 1 '10 at 20:47
@Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
– Tomalak
Jun 1 '10 at 20:48
add a comment |
up vote
1
down vote
up vote
1
down vote
^(?=.*selector=size)(?:(?!details.cfm).)+$
If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:
^[^?]*+(?<!details.cfm).*?selector=size.*$
^(?=.*selector=size)(?:(?!details.cfm).)+$
If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:
^[^?]*+(?<!details.cfm).*?selector=size.*$
edited Jun 1 '10 at 20:35
answered Jun 1 '10 at 20:27
Tomalak
254k51422538
254k51422538
This assumesselector=size
is always beforedetails.cfm
, which isn't the case in the last url.
– Kobi
Jun 1 '10 at 20:34
Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
– Kobi
Jun 1 '10 at 20:47
@Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
– Tomalak
Jun 1 '10 at 20:48
add a comment |
This assumesselector=size
is always beforedetails.cfm
, which isn't the case in the last url.
– Kobi
Jun 1 '10 at 20:34
Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
– Kobi
Jun 1 '10 at 20:47
@Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
– Tomalak
Jun 1 '10 at 20:48
This assumes
selector=size
is always before details.cfm
, which isn't the case in the last url.– Kobi
Jun 1 '10 at 20:34
This assumes
selector=size
is always before details.cfm
, which isn't the case in the last url.– Kobi
Jun 1 '10 at 20:34
Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
– Kobi
Jun 1 '10 at 20:47
Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
– Kobi
Jun 1 '10 at 20:47
@Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
– Tomalak
Jun 1 '10 at 20:48
@Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
– Tomalak
Jun 1 '10 at 20:48
add a comment |
up vote
0
down vote
I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).
My original command:
tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '
Now becomes (with "-P" perl switch):
tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'
add a comment |
up vote
0
down vote
I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).
My original command:
tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '
Now becomes (with "-P" perl switch):
tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'
add a comment |
up vote
0
down vote
up vote
0
down vote
I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).
My original command:
tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '
Now becomes (with "-P" perl switch):
tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'
I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).
My original command:
tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '
Now becomes (with "-P" perl switch):
tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'
edited Jun 16 '16 at 11:33
answered Jun 16 '16 at 11:11
roon
11
11
add a comment |
add a comment |
up vote
-4
down vote
Simple way to do this is to specify 0 instances of the string by doing the following
(string_to_exclude){0}
2
This does not work.
– Austin Henley
Oct 10 '12 at 20:06
this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
– Zoey Hewll
Apr 4 '17 at 5:01
add a comment |
up vote
-4
down vote
Simple way to do this is to specify 0 instances of the string by doing the following
(string_to_exclude){0}
2
This does not work.
– Austin Henley
Oct 10 '12 at 20:06
this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
– Zoey Hewll
Apr 4 '17 at 5:01
add a comment |
up vote
-4
down vote
up vote
-4
down vote
Simple way to do this is to specify 0 instances of the string by doing the following
(string_to_exclude){0}
Simple way to do this is to specify 0 instances of the string by doing the following
(string_to_exclude){0}
edited Jul 27 '12 at 11:53
Taryn♦
187k45284348
187k45284348
answered Jul 27 '12 at 1:36
Trace Johnson
3
3
2
This does not work.
– Austin Henley
Oct 10 '12 at 20:06
this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
– Zoey Hewll
Apr 4 '17 at 5:01
add a comment |
2
This does not work.
– Austin Henley
Oct 10 '12 at 20:06
this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
– Zoey Hewll
Apr 4 '17 at 5:01
2
2
This does not work.
– Austin Henley
Oct 10 '12 at 20:06
This does not work.
– Austin Henley
Oct 10 '12 at 20:06
this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
– Zoey Hewll
Apr 4 '17 at 5:01
this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
– Zoey Hewll
Apr 4 '17 at 5:01
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2953039%2fregular-expression-for-a-string-containing-one-word-but-not-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown