Results 1 to 10 of 10
  1. #1
    neha_sinha is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  3. #3
    neha_sinha is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by neha_sinha View Post
    its again giving the incorrect answer(i.e. including 201.56.78.90 also)...why appending '^' to the string is not working....
    Print the resulting regular expression 'text1' before you compile and apply it; show us the result.

    kind regards,

    Jos

  5. #5
    neha_sinha is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    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..
    Java 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");
    	        				 
    	        				
    	        				 
    	
    	}
    
    }
    its again showing matched

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by neha_sinha View Post
    thanks for replying again

    before Pattern.compile statement the value that text1 contains is:
    ^1*.*.*.*
    That regular expression is not correct; it reads: match the start of the text (^) followed by zero or more '1' characters (1*) followed by zero or more 'anything' characters (.*) etc. etc.

    kind regards,

    Jos

  7. #7
    neha_sinha is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by neha_sinha View Post
    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
    So you are trying to transform something else to a (valid) regular expression? What is the syntax of that 'something else'?

    kind regards,

    Jos

  9. #9
    neha_sinha is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    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 :)

    Java 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");
    	        				 
    	        				
    	        	
    	
    	}
    
    }
    thanks

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by neha_sinha View Post
    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 :)
    [ snip ]
    thanks
    You're welcome of course, glad you fixed it yourself. I didn't read all that code so all I can do is believe your claim about its correctness ;-)

    kind regards,

    Jos

Similar Threads

  1. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 12:15 PM
  2. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 04:55 AM
  3. Java Regular Expression help
    By royalibrahim in forum Advanced Java
    Replies: 11
    Last Post: 11-12-2009, 02:27 AM
  4. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 01:25 PM
  5. I can't find the right regular expression
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •