Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ReGex basic analyze  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can someone explain pattern value at 19th line. What it means? How it is used?

Also how those groups are created and what they mean inside 'if loop' ?

matcher is also interesting stuff, is it only used at ReGex?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
Can someone explain pattern value at 19th line. What it means? How it is used?


The first group will match everything. The second group will match a single digit. And the third group will match nothing. So, during the match, the first group initially takes everything, and then, has to back-off, so that the second group will have a single digit to match.

So, after the find() method, the second group will match the last digit, the first group will match everything just prior to the last digit. And the third group will match nothing.

bob john wrote:
Also how those groups are created and what they mean inside 'if loop' ?


Well, since the "if" statement is not loop, I am not sure what you mean either.... perhaps, it would be a good idea to quote your source?

Henry

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm just going to restate your explanation because I find "first", "second", ..., to be a bit ambiguous when you are talking about things that are zero based.
group(0) // the entire string, in your example 'line'
group(1) // any characters that precede a single digit
group(2) // a single digit
group(3) // nothing (I'm not sure this part of the pattern is legal but I haven't tried it) at any rate, it doesn't seem to be particularly useful
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"regex" stands for regular-expression. so if you insist on camel-case, it would be "RegEx".
 
bob john
Ranch Hand
Posts: 116
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on your PDF, this is what the pattern is supposed to be

group(0) // the entire string, in your example 'line'
group(1) // any characters that precede a group of one or more digits
group(2) // one or more digits
group(3) // any characters remaining after the group of digits

Ok, so now I created my own small test program and there's some subtleties cause by the ".*" component being "greedy" (sorry, don't have time to explain that, you'll need to look up the documentation).

Enter line: 123
0: "123"
1: "12"
2: "3"
3: ""

The first "(.*)" gobbles up some digits because it's is greedy but leaves one behind in order to match "(\\d+)".

Enter line: xxx123xxx456xxx
0: "xxx123xxx456xxx"
1: "xxx123xxx45"
2: "6"
3: "xxx"

If you want to see the non-greedy form of the expression use this regex
"(.*?)(\\d+)(.*?)"

Enter line: 123
0: "123"
1: ""
2: "123"
3: ""

Enter line: xxx123yyy456zzz
0: "xxx123"
1: "xxx"
2: "123"
3: ""

Now you see (0) no longer returns all of 'line', and (3) no longer returns the remaining portion of the string.

If you use only non-greedy for the first .*
"(.*?)(\\d+)(.*)"

Enter line: 123
0: "123"
1: ""
2: "123"
3: ""

Enter line: xxx123yyy456zzz
0: "xxx123yyy456zzz"
1: "xxx"
2: "123"
3: "yyy456zzz"

This was the behavior I was anticipating in the original regex before I was reminded of the greedy behavior.

Basically, regex is very complex, if you use it be sure to test the heck out of it.

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PDF wrote:Following example illustrates how to find a digit string from the given alphanumeric string

This is a horrible example. Although it shows the output correctly in the PDF it is not in line with the goal as quoted above.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!