Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regular expression pattern

 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a shell script which sets various environment variables.
We are interested in one line in that file as follows:

GATEWAY=devsol1:5340;export GATEWAY

Here the ':5340' may or may not be present.

We are trying out a pattern :
Pattern.compile("^(GATEWAY=)(.*)(( ([0-9]+)?)(;.*)($)")
Although this seems to work fine, we needed to confirm whether this is indeed the correct way to capture our used-case.

When we specify the grouping as '(( ([0-9]+)?)' does the '?' apply to just '([0-9]+)' or '( ([0-9]+)' as a group?
[ June 02, 2008: Message edited by: Ranadhir Nag ]
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the latter, but it is difficult to read your post. Please click the pencil-and-paper "edit" icon and tick "disable smilies" at the lower left to get rid of the extraneous
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a closer look, I think I was mistaken.

In ([0-9]+)? the ? applies to ([0-9]+).
 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How then we state that '( ([0-9]+)' may or may not be present as a group ; and yet refer to both '( ' and '([0-9]+)' separately as 'group' through matcher?
An exammple output from the matcher would be as follows:
group 2 - devsol1
group 3 - :
group 4 - 5340
[ June 02, 2008: Message edited by: Ranadhir Nag ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic