Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java regex pattern

 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the string we want to trap and the pattern we use are as follows:

static CharSequence inputStr = "GATEWAY=gate:2000; export GATEWAY ;";
static String patternStr = "^(GATEWAY=)(.*)((([0-9]+))(;.*)($)";

With the matcher ,this works perfect and we get teh groups as follows when printed out:

item 0 : GATEWAY=tgate:2002; export GATEWAY ;
item 1 : GATEWAY=
item 2 : tgate
item 3 : :2002
item 4 : :
item 5 : 2002
item 6 : ; export GATEWAY ;
item 7 :

However,in the 'gate:2000' the ':2002' part is optional.
So we have to tweak the pattern so that it acknoledges the optionality of the ':<port>' bit.

This is something we are not able to figure out.
can someone help on how to tweak the patternStr to get an output like below, when the ':<port>' is ignored :

original string :
GATEWAY=tgate; export GATEWAY ;

outpt:

item 0 : GATEWAY=tgate; export GATEWAY ;
item 1 : GATEWAY=
item 2 : tgate
item 3 :
item 4 :
item 5 :
item 6 : ; export GATEWAY ;
item 7 :
 
youri lima
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the ? is used for making condition optional in regex so you can put that behind the part you dont require to be present.

how about something like

"^(GATEWAY=)(.*)((:)([0-9]+))?(;.*)($)"

the ? applies only for the previous condition given. the brackets enclose a serie of condition making it one condition so the ? applies for the entire regex enclosed within the brackets... (eg. "((:)([0-9]+))"
 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried this earlier .
It distorts the results of teh previos used case too!!!



item 0 : GATEWAY=tgate:2002; export GATEWAY ;
item 1 : GATEWAY=
item 2 : tgate:2002; export GATEWAY
item 3 :
item 4 :
item 5 :
item 6 : ;
item 7 :
 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even fr the used case with 'no port' the output comes as follows,which is not what we are seeking:

item 0 : GATEWAY=tgate; export GATEWAY ;
item 1 : GATEWAY=
item 2 : tgate; export GATEWAY
item 3 :
item 4 :
item 5 :
item 6 : ;
item 7 :
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try...

^(GATEWAY=)(.*?)((:)([0-9]+))?(;.*)($)



Ranadhir Nag wrote:I had tried this earlier .
It distorts the results of teh previos used case too!!!


It doesn't "distorts the results" of the previous case, it is that the previous case no longer have something to stop it from grabbing too much, since the port is optional.


Henry
 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do we prevent it from doing so - which essentially means preventing group 2 not to contain a ';' - or rather stop before a ';'
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranadhir Nag wrote:how do we prevent it from doing so - which essentially means preventing group 2 not to contain a ';' - or rather stop before a ';'


I gave you the solution in my post. Did it not work?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!