sobota, 10 września 2011

REGEX

1.

 [a-f] search for a, b, c, d, e or f
 [af] search for a or f
 \w search for [a-zA-Z_0-9]
 \s  search for space
 \d search for [0-9]
 . search for any character
\. search for dot

? - 0 or 1 time
* - 0 or more times
+ - 1 or more times
{n} - exactly n times
{n, } - minimum n times
{n, m} - minimum n times and maximum m times
------------------------------------------------------------------------------------------------------------

2. If one tries to use group/start/end method of Matcher class and the searched string is not found, the IllegalStateException is thrown, e.g.:


        Pattern p = Pattern.compile("test");
        Matcher m = p.matcher("my text");
     
        m.find();
        System.out.println(m.group() + " " + m.start() + " " + m.end());


Keep in mind that you need use find method first and then group/start/stop, eg.


        Pattern p = Pattern.compile("test");
        Matcher m = p.matcher("test");
     
        System.out.println(m.group() + " " + m.start() + " " + m.end());

In both cases IllegalStateException is trhown.


The proper useage may be as follows:


        Pattern p = Pattern.compile("test");
        Matcher m = p.matcher("my text");
     
        while(m.find()){
            System.out.println(m.group() + " " + m.start() + " " + m.end());
        }





Brak komentarzy:

Prześlij komentarz