Feb 28

Today the last example for (non) greedy operators:

Pattern p = Pattern.compile(“ckc.+”);
Matcher m = p.matcher(“c2ckcckcx9ckckx7c”);
boolean b = false;
while (b=m.find())

System.out.println(m.start() +” “+m.group());

‘.’ is still the replacement for one character, with ‘+’ we are searching for one or unlimited Strings.

Therefore the Output is:

2 ckcckcx9ckckx7c

So first of all no difference to ‘*?’.  Let’s have a look on the change to non-greedy:

Pattern p = Pattern.compile(“ckc.+?“);

Here the Output:

2 ckcc
10 ckck

For comparison, while using ‘*’ (zero, one or unlimited appearance) the non-greedy output would be:

2 ckc
5 ckc
10 ckc

Tagged with:
Feb 27

After focusing yesterday on ‘*?’ I would like to explain ‘??’ and ‘+?’.

Let’s start today with ‘??’:

Pattern p = Pattern.compile(“ckc.?”);
Matcher m = p.matcher(“c2ckcckcx9ckckx7c”);
boolean b = false;
while (b=m.find())

System.out.println(m.start() +” “+m.group());

‘.’ is again the replacement for any other character, ‘?’ means that this String should be there zero or one times.

Therefore the Output is:

2 ckcc
10 ckck

After change to non-greedy:

Pattern p = Pattern.compile(“ckc.??“);

the output is:

2 ckc
5 ckc
10 ckc

Tagged with:
Feb 26

Today I would like to explain what are (non) greedy operators and explain how to distinguish. As usual, let’s start with an example:

Pattern p = Pattern.compile(“ckc.*”);
Matcher m = p.matcher(“c2ckcckcx9ckckx7c”);
boolean b = false;
while (b=m.find())

System.out.println(m.start() +” “+m.group());

As already explained is ‘.’ the replacement for any other character, with ‘*’ we are searching for zero, one or unlimited of the String ‘ckc’. In combination we are searching for the String ‘ckc’ + further characters due to ‘.’

Therefore the Output is:

2 ckcckcx9ckckx7c

So everything is given out, the operator is greedy. More or less the order was to give out the longest possible String. In order to change this and having a non-greedy operator, a ‘?’ is added:

Pattern p = Pattern.compile(“ckc.*?“);

Now the Output is:

2 ckc
5 ckc
10 ckc

Tagged with:
Feb 25

‘.’ is the replacement for any other possible character.

Pattern p = Pattern.compile(“1.3″);
Matcher m = p.matcher(“123 13 12 1c3″);
boolean b = false;
while (b=m.find())

System.out.println(m.start() +” “+m.group());

Therefore the output is:

0 123
10 1c3

Tagged with:
Feb 24

As you might already know you can also put together the different statements, eg

Pattern p = Pattern.compile(“\\w\\w\\w([-\\s])\\w\\w”);
Matcher m = p.matcher(“ccc-ccccc”);
boolean b = false;
while (b=m.find())

System.out.println(m.start() +” “+m.group());

A substring will be searched which contains 3 chars and than exactly one whitespace or a ‘-’ and than again 2 chars.

Therefore the output is: 0 ccc-cc

Should be more than one whitespace or ‘-’ be allowed we need to adjust the statement:

Pattern p = Pattern.compile(“\\w\\w\\w([-\\s])*\\w\\w”);


Tagged with:
Feb 23

In case you need to give out all signs after a given sign (in our case still ‘c’) there is also a solution for that:

Pattern p = Pattern.compile(“c([^,])*”);
Matcher m = p.matcher(“cccxcc”);
boolean b = false;

while (b=m.find())

System.out.println(m.start() +” “+m.group());

Therefore the output is:

0 cccxcc

Tagged with:
Feb 22

With ‘+’ we are searching for one or unlimited ‘c’:

Pattern p = Pattern.compile(“c+”);
Matcher m = p.matcher(“cccxcc”);
boolean b = false;
while (b=m.find())

System.out.println(m.start() +” “+m.group());

In contradiction to previous examples, if ‘c’ does not occur, nothing will be found. Therefore the Output is:

0 ccc
4 cc

Tagged with:
Feb 21

Today we are searching for zero or unlimited ‘c’:

Pattern p = Pattern.compile(“c*”);
Matcher m = p.matcher(“cccxcc”);
boolean b = false;
while (b=m.find())

System.out.println(m.start() +” “+m.group());

In contradiction to ‘?’ not every ‘c’ is found one by one, but grouped. Therefore the Output is:

0 ccc
3
4 cc
6

Tagged with:
Feb 20

Today e are starting to have a look on quantifiers, starting with ‘?’ and an example about this:

Pattern p = Pattern.compile(“c?”);
Matcher m = p.matcher(“cccxcc
“);
boolean b = false;

while (b=m.find())

System.out.println(m.start() +” “+m.group());

Here, everything will be found which contains zero or one ‘c’:

0 c
1 c
2 c
3
4 c
5 c
6

Therefore the matcher returns an every position “true”, but method group is only returning the value of the position which actually contains ‘c”

Tagged with:
Feb 19

Let’s go to the even more complex issues; we put mixed expressions and grouped findings together:

Pattern p = Pattern.compile(“([c][i-k])+”);
Matcher m = p.matcher(“12ckckck43ck”);
boolean b = false;
while (b=m.find())

System.out.print(m.start() + ” “);

Strings one after another are disregarded. Therefore the output is: 2 10

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs