Forum Moderators: open
I want to return all lines with "do" that don't contain "that" (without knowing the content of all the lines in all my files).
I have tried this, but it doesn't seem to work:
do.*[^that]
How do you specify to NOT match an entire string (like "that", for example) while still matching another string (like "do") in the same regexp?
Note i am not using perl or anything like that, just searching in eclipse so please don't give me any whacky syntax =) Thanks in advance.
Anyway ... regardless of language, the easiest way of doing this is to simply check if it matches word A but not word B separately. So, something like:
if(insert_regexp_matching_function_name(/(^¦[^a-z])do([^a-z]¦$)/i) && !insert_regexp_matching_function_name(/(^¦[^a-z])that([^a-z]¦$)/i)) {
// we have a match
} [edited by: DrDoc at 7:25 pm (utc) on Jan. 21, 2006]
/(^¦[^a-z])((that[^a-z](.*[^a-z])?)do¦do([^a-z]that([^a-z].*)?))([^a-z]¦$)/i ... but that's a little bit messy. (The above one matches if the string contains both "do" and "that", regardless of order.)
Since this is the JavaScript forum, I decided to put together a test example in JavaScript. Since you may use another language, the code is not important. But at least it demonstrates the functionality.
<script type="text/javascript">
sentences = new Array('I do not like her.',
'I like to do this.',
'I like to do that.',
'Do what you like.',
'That I do like.',
'He likes to do that.',
'Do.',
'Do that.');
for(i = 0; i < sentences.length; i++) {
// if(sentences[i].match(/(^¦[^a-z])do([^a-z]¦$)/i) && !sentences[i].match(/(^¦[^a-z])that([^a-z]¦$)/i)) {
if(!sentences[i].match(/(^¦[^a-z])((that[^a-z](.*[^a-z])?)do¦do([^a-z]that([^a-z].*)?))([^a-z]¦$)/i)) {
document.write("<b>" + sentences[i] + "</b><br>");
}
else {
document.write("<i>" + sentences[i] + "</i><br>");
}
}
</script>
Toggle between the two
[b]if[/b] statements.
I tried that script and it just writes out all 8 sentences.
I believe it is meant to. The sentences that match are printed bold. Those that don't are italic.
Doc's regexp's worked perfectly for me.
so i can't split the search into 2
In that case, use the regexp in the demo that does it all at once, ie:
[blue]/^¦[^a-z])((that[^a-z](.*[^a-z])?)do¦do([^a-z]that([^a-z].*)?))([^a-z]¦$)/i[/blue] /* change corrupted ¦¦ chars for pipes */
I'd go for the second regexp I posted earlier ... provided you can negate the result, that is.
so i want to find all of the first kind (but i don't know about the exact number of attributes in all such tags...)
it is so strange, because this concept is conceptually so simple:
does the line contain X?
if yes
does it not contain Y?
if yes
add to list of matches.
the people who maintain regex should take a look at this problem and implement a simple solution for it, so that something like this could be run:
html:text.*!(maxlength)+
instead of some massively long sentence of tricky syntax, but hey, that's just my opinion =)
onload = function()
{
var wonder = new WebPageWonderfulness;
wonder.doStuffWhileISitBackAndHaveANiceCupOfTea();
}
Anyway. Having trouble with this. It must be a common one, and I have a sneaky feeling I solved it once, sometime, somewhere.
Quite likely to have something to do with negative lookahead.
[blue](?!maxlength)[/blue]
<script type="text/javascript">
sentences = new Array('I do not like her.',
'I like to do this.',
'I like to do that.',
'Do what you like.',
'That I do like.',
'He likes to do that.',
'Do.',
'Do that.',
'Do this and that all the time.');
for(i = 0; i < sentences.length; i++) {
if(sentences[i].match(/(^¦[^a-z])do(??that[^a-z])([^a-z]¦$)/i)) {
document.write("<b>" + sentences[i] + "</b><br>");
}
else {
document.write("<i>" + sentences[i] + "</i><br>");
}
}
</script>
Bold = should match
Italic = should not match
Compare with output.