tags:

views:

162

answers:

7

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

+2  A: 

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().

Samir Talwar
@Samir Talwar I'm using it inside eclipse, how do I use command line isntead ?
Gandalf StormCrow
Go into your "bin" directory (or wherever Eclipse is outputting your ".class" files) using the `cd` command and run `java MyClassName`. If the class is in a package, prefix it: `java my.package.name.MyClassName`. If that doesn't work, should and tell us what OS you're using.
Samir Talwar
A: 

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!

Martijn Courteaux
What is wrong? Why -1?
Martijn Courteaux
Dunno I didn't vote you down +1
Gandalf StormCrow
@Gandalf: Thank you!
Martijn Courteaux
A: 

I suggest you to learn regex with Perl. Its much better integrated than with Java

bertolami
A: 

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.

scompt.com
A: 

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.

mkorpela
+1  A: 

I highly suggest doing two things:

  1. Read the first two chapters of Mastering Regular Expressions
  2. 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
Cole
+1  A: 

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

cad