Tuesday, October 19, 2010

Free Playsets Blueprint

API - API

Le classi per lavorare su pattern in Java sono:

  • Pattern e Matcher e String.split per lavorare su stringhe;
  • Formatter e Scanner per lavorare su stream.
Quando si cerca un determinato pattern all'interno di una stringa si possono usare le regular expression; per usarle si crea un Pattern con l'espressione da cercare, e poi si cerca su un testo:
    Pattern p = Pattern.compile("o."); // the expression
    Matcher m = p.matcher("ciao mondo"); // the source
   while (m.find ()) {System.out.println
(m.start () + "" + m.end () + "\u0026lt; , "+ m.group () + ">");
}
print
3 5 \u0026lt;or>
6 8 \u0026lt;ON>
methods Matcher.start () returns the current position in Matcher (Where the match was found today), Matcher.end () returns the final position, and Matcher.group () returns the entire contents . The method Matcher.group () also allows you to choose the subgroup sought in the case of more complex regular expressions, look at the following example:
Pattern p = Pattern.compile (" (.) (or )(.)"); / / the expression
Matcher m = p.matcher ("hello world"), / / \u200b\u200bthe source
; while (m.find ())
{System.out.println (m.start () + "" + m.end () + "\u0026lt;" + m.group (1) + "- "+ m.group (2) +" - "+ m.group (3) +} ">");
returns:
\u0026lt;a-o-2 5> 5 8
\u0026lt;m-o-n>
because the expression (.) (o) (.) finds all locations where we have an 'o' surrounded by two characters, and then divide the result into three groups, from which 'a', 'o', '' in ' hello ".

The next step is to use wildcards, we have the following:
  • \\ d - is the digits, ie digits 0-9
  • \\ w - the word is, that the characters (letters (from 'a' and 'z') and (between 'A' and 'Z') and (between 0 and 9) or '_')
  • \\ s - is the space ('', '\\ n', '\\ r', '\\ t')
or we can use the '[' and ']' to encompass the range of characters, For example \\ d is equal to [0-9] and \\ W is equal to [a-zA-Z0-9_] we can use quantifiers to indicate how many elements we look for: if we use '+' indicates (at least) if we use '*' indicates (between zero and infinity) and if we use '?' index (zero or one) if we use [^ abc] indicate (all except 'a', 'b' or 'c') if we show we can use any character '.', then if we do not greedy search we use '*?' instead of '*' and '? +' instead of '+', the difference between greedy and greedy search is that the larger the match, while not looking the least greedy, an example is
Pattern greedy : .* xx
Pattern not greedy: .*? Xx
String: yyxxxyxx
the greedy match (greedy) as a result of " yyxxxyxx ", while the match is not to be greedy as a result yyxx and xyxx

How Matcher even Scanner allows you to search, working on Stream (or File or String):
; Scanner s = new Scanner ("hello world");
String token;
    do {
        token = s.findInLine("(.)(o)(.)");
        System.out.println("found <" + token + ">");
    } while (token != null);

Scanner normalmente viene utilizzato per fare il " tokenizing "; l'alternativa is to use String.split () , while we see how to do this and then with Scanner:
String [] str = "hello world are very happy." split (". or .)
for (String s: str)
System.out.println (s + "-");
that returns us
- - - - o - - happy -
As we get la stessa funzione dello scanner, abbiamo spesso una funzionalità non richiesta: lui esegue il tokenizing di tutta la stringa prima di darci un output; quando questo non è il comportamento desiderato basta utilizzare lo Scanner (che funziona anche su Stream e File, e restituisce tipo primitivi):
    Scanner s = new Scanner("ciao 123 cmondo true sdada");
    while(s.hasNext()) {
    if (s.hasNextInt())
        System.out.print("Intero(" + s.nextInt() + "); ");
    else if (s.hasNextBoolean())
        System.out.print("Boolean(" + s.nextBoolean() + "); ");
    else
        System.out.print(s.next() + "; ");
    }
returns:
hello, Whole (123); cmondo; Boolean (true); Sdad;

We finally Formatter object that provides methods printf and format that behave like this:
...

The code is here

0 comments:

Post a Comment