03. august 2006 - 11:18
Der er
9 kommentarer og 1 løsning
Help with a Reg Exp!
Hello! I need to verify that a String looks like: 1111.2222.333.4444 Description. The string must contaion 3 "." and can contain any number between 1-9 between the dots so for eg this would be ok: 1234567890.2.4567890.3 I can't get my Pattern in Java correct so if you guys know how to solve this, that I guess should be simple, please let me know. Best regards Fredrik
Annonceindlæg tema
03. august 2006 - 11:41
#1
Someting like ^\d+\.\d+\.\d+\.\d+$ And then escape the \'s properly in Java strings.
03. august 2006 - 11:52
#2
Hello! I tried it like Pattern p = new Pattern("^\\d+\\.\\d+\\.\\d+\.\\d+$"); And Pattern p = new Pattern("\\d+\\.\\d+\\.\\d+\.\\d+$"); Strangely both seems to work? What does the ^ do? My guess is that the ^ means like ! in java programming, am I wrong? Best regards Fredrik
03. august 2006 - 11:52
#3
... or like this: ^\d+(\.\d+){3}$
03. august 2006 - 11:54
#4
^ is an anchor that matches "the start of the line". Without it, you will get false positeves: "something 111.222.33.44" - will match.
03. august 2006 - 11:55
#5
Likewise $ is an acnhor that matches "the end of the line".
03. august 2006 - 11:56
#6
Or worse in your situation: 111.222.333.444.555.666.777 - will match
03. august 2006 - 11:56
#7
Okey!! Thanks alot guys! Give svar so I can reward you! Best regards Fredrik
03. august 2006 - 11:58
#8
I guess that you ended up using erikjacobsen's solution - so I'll skip on this one. :^)
03. august 2006 - 12:00
#9
I don't collect points, tnx.
30. december 2006 - 13:34
#10
Ok, then I close this one
Kurser inden for grundlæggende programmering