Results 1 to 10 of 10
Thread: Can someone explain ReGex ?
- 12-03-2016, 08:36 PM #1
Senior Member
- Join Date
- Nov 2016
- Posts
- 163
- Rep Power
- 4
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"); } } }
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?
- 12-03-2016, 08:55 PM #2
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.htmlIf you don't understand my response, don't ignore it, ask a question.
- 12-03-2016, 09:17 PM #3
Senior Member
- Join Date
- Nov 2016
- Posts
- 163
- Rep Power
- 4
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.
- 12-03-2016, 09:20 PM #4
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.
- 12-03-2016, 09:44 PM #5
Senior Member
- Join Date
- Nov 2016
- Posts
- 163
- Rep Power
- 4
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 (); } }
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
- 12-03-2016, 09:47 PM #6
Re: Can someone explain ReGex ?
how to split this line.
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.
- 12-04-2016, 01:46 PM #7
Senior Member
- Join Date
- Nov 2016
- Posts
- 163
- Rep Power
- 4
- 12-04-2016, 01:57 PM #8
Re: Can someone explain ReGex ?
how to split this line.
On the road is: car, in the car is - 3 humans.
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.
- 12-04-2016, 02:09 PM #9
Senior Member
- Join Date
- Nov 2016
- Posts
- 163
- Rep Power
- 4
Re: Can someone explain ReGex ?
substring (21,29) can do this.
But i dont know how to perform that with split() method?
- 12-04-2016, 02:12 PM #10
Similar Threads
-
Don't know how to explain this
By Jhaz in forum New To JavaReplies: 0Last Post: 08-16-2014, 01:45 PM -
please explain
By shad0wblade890 in forum EclipseReplies: 7Last Post: 03-14-2014, 01:47 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 2Last Post: 12-13-2010, 02:34 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 3Last Post: 12-13-2010, 08:22 AM -
Can someone explain why...
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-19-2009, 07:59 AM
Bookmarks