Daniel Croft wrote:I believe this is what you want: "A\\d{6}((?!2008).)*$" I ran it on your examples and it worked, but I would try it with a few more because I'm not 100% sure about it, my regex skills are a bit rusty.
The A catches your first A, \d{6} stands for "any 6 digits" but you need the extra \ because java will recognize the first \ as an escape character. ((?!2008).)*$ means "any number of characters that don't contain 2008". Hope this works for you =)
Ricky Murphy wrote:I have been struggling to achive following by using regex in Java:
For example. if I have following 5 sentences:
1. A123456 some 2008 junk in my PC
2. A234567 another pile of 2008 junk
3. A345678 collected 2009 rocks
4. A456 got that in 2009.
5. A567890 sent me 3 letters.
What I want to achieve is: pick out the sentences that each should:
a). begins with A and followed by 6 digits
b). does NOT have the word 2008 in it.
This should give me sentence 3) and 5).
How do I construct a regex patten?
Ricky Murphy wrote:
I think that I know what I did wrong (but not why). I started with the "positive" comparison:
a). begins with A and followed by 6 digits
b). AND has the word 2008 in it.
So i ended up with regex: ^A\d{6}\s.*(2008).*$ and ( ^A\d{6}\s.*((2008).)*$ works too )
Ricky Murphy wrote:
While above worked for me, I thought the (?!2008) would give what I originally wanted ( i.e. the regex: ^A\d{6}\s.*(?!2008).*$ ). Wrong, not the case, with added ?! I still got a match.
But to answer why it always matches.... the first ".*" will basically greedily take the whole string, leaving the negative look ahead to always not match (meaning succeed), and the last ".*" to always have a zero length match.
First of all, it is a negative look ahead. It is not a negative match..... ie. "(?!2008)" is not the opposite case of "(2008)".
Ricky Murphy wrote:
While ^A\d{6}((?!2008).)*$ gives me good negative filtering,
Ricky Murphy wrote:
^A\d{6}((?=2008).)*$ does NOT give me any positive findings
Ricky Murphy wrote:
I tried other combinations too, such as ((?=(2008)), etc.
Ricky Murphy wrote:Thank you Daniel and Henry.
I think that I know what I did wrong (but not why). I started with the "positive" comparison:
a). begins with A and followed by 6 digits
b). AND has the word 2008 in it.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Don't listen to Steve. Just read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|