Results 1 to 10 of 10
  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Can someone explain ReGex ?

    Java Code:
    package DO;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class SubStr
    {
    	 public static void main( String args[] ) throws FileNotFoundException{
    	 
    		 File file = new File ("kontrolinis");
    		 Scanner sken = new Scanner (file);
    	
    		 
    	
    	 String line = sken.nextLine ();
    	
    	 
    	 String pattern = "(.*)(\\d)(.*)";
    	
    	
    	 Pattern r = Pattern.compile(pattern);
    	
    	 
    	
    	 Matcher m = r.matcher(line);
    	
    			 if (m.find( )) {
    			 System.out.println( m.group(0) );
    			 System.out.println( m.group(1) );
    			 System.out.println( m.group(2) );
    		 }
    		 
    					 else {
    				 System.out.println("NO MATCH");
    				 }
    		
    	 }
    
    }
    Text file:
    Toyota; auris - 4.0; 5
    Audi ; 100 - 1.8; 3
    BMW ...
    ...


    The questions comes about this code.
    I dont understand 20th line 'String pattern' value. Why so many () ?

    23th line is also confusing, what pattern.compile do?

    Matcher somehow create these groups too?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Can someone explain ReGex ?

    Pattern matching and regular expressions are more advanced topics.

    What are you trying to do that needs them?

    There is some discussion about regular expressions in the Pattern class. See the API doc for the Pattern class: Pattern (Java Platform SE 8 )
    Also see the tutorial: http://docs.oracle.com/javase/tutori...gex/index.html
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Can someone explain ReGex ?

    For example text file contains:
    The bird flies around.
    The beer walks around.


    Combing loop and regex, there should be possible to get console output :
    The bird flies
    The beer walks


    So I wish to know how to make.
    Perhaps even possible to skip Integer values and so on.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Can someone explain ReGex ?

    What have you tried?

    Look at the samples on the Pattern class's API doc and the String class's split method?

    This seems like an advanced assignment for a beginning student.

    I suggest you work with the String class's methods to find and extract parts of a String to create other Strings.
    Last edited by Norm; 12-03-2016 at 09:33 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Can someone explain ReGex ?

    Java Code:
    package DO;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class Splits {
       
        public static void main(String[] args) throws FileNotFoundException {
       
       Scanner sn = new Scanner ( new File ("just")    );
        
        
        while ( sn.hasNext () )
        {
           String om = sn.nextLine ();
           String[] sentenceArray = om.split(":");
        
           System.out.println ( sentenceArray[1]  );  
        }
        
     sn.close ();       
                 
                 
        }
    }
    With split is fine
    However i wish to know how to split this line.
    Text:
    On the road is: car, in the car is - 3 humans.

    How to split it twice and have output result:
    in the car

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Can someone explain ReGex ?

    how to split this line.
    I'd use the String class's methods to locate the desired sub string and extract it.

    What is in the sentenceArray array after the split? See the Arrays class's toString method for printing an array's contents.
    Last edited by Norm; 12-03-2016 at 09:49 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Can someone explain ReGex ?

    Quote Originally Posted by Norm View Post

    What is in the sentenceArray array after the split? See the Arrays class's toString method for printing an array's contents.
    sentenceArray [0] text before " : "
    [1] text after " : "

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Can someone explain ReGex ?

    how to split this line.
    On the road is: car, in the car is - 3 humans.
    You need a way to find the two boundaries for the desired text you want to extract. What comes before it and what comes after it?
    I think the String class's methods would work better than a regular expression.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Can someone explain ReGex ?

    substring (21,29) can do this.
    But i dont know how to perform that with split() method?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Can someone explain ReGex ?

    how to perform that with split() method?
    I'm not sure a regular expression with the split method is the best way to solve that problem.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Don't know how to explain this
    By Jhaz in forum New To Java
    Replies: 0
    Last Post: 08-16-2014, 01:45 PM
  2. please explain
    By shad0wblade890 in forum Eclipse
    Replies: 7
    Last Post: 03-14-2014, 01:47 PM
  3. Replies: 2
    Last Post: 12-13-2010, 02:34 PM
  4. Replies: 3
    Last Post: 12-13-2010, 08:22 AM
  5. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 07:59 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
  •