Trying to learn regex [closed]
Hello everyone I'm trying to learn regex from java website and I try to run this JAVA program but each time I get No Console output, what do I do ? here is the url :
http://java.sun.com/docs/books/tutorial/essential/regex/test_harness.html
I'm trying this inside eclipse
UPDATE :
Can anybody suggest some other better way to learn regex
Are you running the application in an IDE such as Eclipse or IntelliJ IDEA? Try using the command line instead. Some IDEs will screw up calls to System.console()
.
I highly suggest doing two things:
- Read the first two chapters of Mastering Regular Expressions
- Buy Regex Buddy. It's a little helper program that holds your hand with using and explaining all the tokens and grouping options. It lets you test your expressions on text you're using and also generates code snippets in many different languages. I started using it when I needed regex solutions and it brought me up the learning curve fast. Now I don't need it, but I still use it to test out some complex named group matches I do for batch file parsing. Regex Buddy Website
I had to learn RegEx one year ago. I used Wikipedia page for regex and these these two sites to test online the regex I needed (Site1, Site2) instead of using regex from UltraEdit...
It is not as difficult as you could think at the beginning
I suggest you to learn regex with Perl. Its much better integrated than with Java
Yes, what Samir said is correct. You can test if your IDE supports the console by doing this:
if (System.console() == null)
{
System.out.println("No console supported! :-(");
} else
{
System.out.println("Console supported! :-)");
}
You can read the java-doc about System.console();
here
There can you see null
will be returned if there isn't a console.
To answer your second question
To print something:
System.out.println("Here your text you want to print");
To read something:
Scanner scanner = new Scanner(System.in);
String oneWord = scanner.next();
String oneLine = scanner.next("\n");
or:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Hope this helps, and you can continue learning regexes!
Use this website: http://www.fileformat.info/tool/regex.htm
Internally, it uses the java.util.regex
package, so you can worry about learning regex and still have your knowledge directly transfer to regex in Java when you need it.
You could try different kinds of regular expressions with eclipses find/replace (or with some none ide text editor that supports regex).
Python or Ruby interactive shells are good places to test regex (and regex syntax is much nicer than in Java).
And of course there's the internet.
精彩评论