• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regex

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I am learning regex and I have a question, which I have not understand the difference

0[xX]([0-9a-fA-F])

0[xX]([0-9a-fA-F]+)

Difference between those two ? Whenmy Matcher is

"Z0XFZQ0XFQZ0XA"

And Pattern is

0[xX]([0-9a-fA-F])+

Complete Program is :

import java.util.regex.*;
class testing{
public static void main(String args[]){
Pattern p = Pattern.compile("0[xX]([0-9a-fA-F])+");
Matcher m = p.matcher("Z0XFZQ0XFQZ0XA");
while(m.find()){

System.out.println(m.start());
}

}

}

The ouput is
1
6
11

I change the pattern to 0[xX]([0-9a-fA-F]) (No + sign)
now also the ouput is

1
6
11

CAN ANYBODY PL EXPLAIN WHAT FOR + sign in this case ? What different output I should get when I use + sign ? Thanks very much
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The plus quantifier allows you to match one or more occurences of a group. I tested some simpler patterns with your regular expressions to illustrate this:

C:\mycode\java\regex>java SimpleRegex groups 0[xX]([0-9a-fA-F]) Z0XF786
Found 0XF from 1 to 4
Found F from 1 to 4

C:\mycode\java\regex>java SimpleRegex groups 0[xX]([0-9a-fA-F]+) Z0XF786
Found 0XF786 from 1 to 7
Found F786 from 1 to 7

In the second case, the 3 numbers 7,8 and 6 are matched because of the plus sign, in the first case, the pattern matched ends at 0XF.

The source code for the java class can be found at Sun Regex Tutorial - Test Harness

- Anu
[ June 15, 2006: Message edited by: Anupama Ponnapalli ]
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
print matcher.group(),
U will understand the diff between +, * etc.,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic