Results 1 to 10 of 10
- 07-17-2010, 10:28 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
Regular expression to find strings beginning with a particular digit
hello everyone,
i am trying to match data by using regular expression.For eg if a user enters 1*
then he should get all the ip address that start with 1
1.23.56.89
123.56.90.67
(note:ip address validation need not be taken care of since data is matched against valid ip addresses)
the pattern that i am providing is
1\d*\.\d+\.\d+\.\d+
but the problem is am not getting the correct answer...the matched answers that am getting are:
201.56.78.90
172.23.45.67
172.34.87.90
Java Code:text1=cmb1_1[0].getSelectedItem().toString(); int count1=0; for(i=0;i<text1.length();i++) { if(text1.charAt(i)=='.') { count1++; } } if(count1==0) { if(text1.charAt(1)=='*') { text1.replace("*","@"); text1=text1+".*"+".*"+".*"; } } for(i=0;i<text1.length();i++) { text1.replace(".","\\."); text1.replace("*","\\d+"); } text1.replaceAll("@","\\d*"); Pattern pattern=Pattern.compile(text1); Matcher matcher=pattern.matcher(rowValue); if(matcher.matches()) System.out.println("matched"+rowValue);
please help me with this..what pattern should i give to search all strings that begin with a particular digit.
thanks
- 07-17-2010, 10:37 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Try to make it search/match from the start of the String; e.g. "^1\d*\.\d+\.\d+\.\d+"
b.t.w. what does this have to do with Swing?
kind regards,
Jos
- 07-17-2010, 11:16 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
thanks for replying...and sorry i do realise that it has nothing to do with swing...i tried out what you suggested but its only working for me if i create the pattern statically assuming that the user will search for only strings beginning with 1
Pattern pattern=Pattern.compile("^1\\d*\\.\\d+\\.\\d+\\.\\ d+");
but if i try to make it dynamic and try appending '^' before the string its not working for me
Java Code:if(count1==0) { if(text1.charAt(1)=='*') { text1.replace("*","@"); [QUOTE] text1="^"+text1+".*"+".*"+".*"; [/QUOTE] } } for(i=0;i<text1.length();i++) { text1.replace(".","\\."); text1.replace("*","\\d+"); } text1.replaceAll("@","\\d*"); System.out.println(text1); Pattern pattern=Pattern.compile(text1);
its again giving the incorrect answer(i.e. including 201.56.78.90 also)...why appending '^' to the string is not working....
thanks.
- 07-17-2010, 11:53 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 07-17-2010, 12:11 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
thanks for replying again
before Pattern.compile statement the value that text1 contains is:
^1*.*.*.*
am posting a short snippet...you can run it and see..
its again showing matchedJava Code:import java.util.regex.Matcher; import java.util.regex.Pattern; public class testStringFunctionality { public static void main(String args[]) { String string="1*"; int count1=0; for(int i=0;i<string.length();i++) { if(string.charAt(i)=='.') count1++; } if(count1==0) { if(string.charAt(1)=='*') { string.replace("*","@"); string="^"+string+".*"+".*"+".*"; } } for(int i=0;i<string.length();i++) { string.replace(".","\\."); string.replace("*","\\d+"); } System.out.println(string); string.replaceAll("@","\\d*"); // Pattern pattern=Pattern.compile("^1\\d*\\.\\d+\\.\\d+\\.\\d+"); Pattern pattern=Pattern.compile(string); System.out.println(pattern); Matcher matcher=pattern.matcher("221.56.78.90"); if(matcher.matches()) System.out.println("matched"); else System.out.println("not matched"); } }
- 07-17-2010, 12:15 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 07-17-2010, 12:21 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
thanks...
could you please suggest what all changes do i need to make to the snippet to have the correct regular expression so that i can get the correct result..am stuck with it...I am replacing '.' by "\\." and '*' by "\\d+"...but still why its giving the wrong value...please do help..
thanks
- 07-17-2010, 12:49 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 07-17-2010, 01:11 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 10
- Rep Power
- 0
hey thanks for all your help...i debugged it myself...actually there was some problem with the implementation of replace method...its working correctly now...here's is the snippet :)
thanksJava Code:import java.util.regex.Matcher; import java.util.regex.Pattern; public class testStringFunctionality { public static void main(String args[]) { String string = "1*"; int count1=0; for(int i=0;i<string.length();i++) { if(string.charAt(i)=='.') count1++; } if(count1==0) { if(string.charAt(1)=='*') { String expression = "*"; String replacement = "@"; string=string.replace(expression, replacement); string="^"+string+".*"+".*"+".*"; } } String expression = "."; String replacement = "\\."; string =string.replace(expression, replacement); expression = "*"; replacement = "\\d+"; string=string.replace(expression, replacement); expression = "@"; replacement = "\\d*"; string=string.replace(expression, replacement); Pattern pattern=Pattern.compile(string); System.out.println(pattern); Matcher matcher=pattern.matcher("221.56.78.90"); if(matcher.matches()) System.out.println("matched"); else System.out.println("not matched"); } }
- 07-17-2010, 01:21 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
regular expression
By prof.deedee in forum JDBCReplies: 3Last Post: 02-19-2010, 11:15 AM -
regular expression
By QkrspCmptPop in forum Advanced JavaReplies: 8Last Post: 01-20-2010, 03:55 AM -
Java Regular Expression help
By royalibrahim in forum Advanced JavaReplies: 11Last Post: 11-12-2009, 01:27 AM -
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 12:25 PM -
I can't find the right regular expression
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks