• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression Problem in java  RSS feed

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I was trying out one regular expression in java but, for some reasons its not working. The test string looks something like this in the eclipse watch window:-

Test string:-
Content-Disposition: form-data; name="name"\n\nOmkar\n|


In the above test string the last character is a pipe symbol.

And the java code i have written is: -



After, the debug line moves over the first line in this method, the value contained in the "parameterRegex" string variable is:-

Regex:-
name="name"(.*)|


However, my SOP statement, prints null....even though, "isFound" variable is true....can someone please explain me what could be the problem, where i am going wrong ?....

I have even, set the DOTALL option for the regex.

please some one help me!

Thanks and Regards
Omkar Patkar
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "matcher.group(0)" return?
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://forum.java.sun.com/thread.jspa?threadID=5307410
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here at JavaRanch, we, too, like folks to be BeForthrightWhenCrossPostingToOtherSites.
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mmm....yes actually, i myself have posted that, but i was ...and i am still desperate in getting solution to the problem. I thought, by the time, i get some idea here, i will post in sun java forum also, i went through the cross posters link in JavaRanch FAQ, and only then i realized, how i am supposed to post my query. Well, folks, i have posted it in sun java forum, also, but even their i did not get much of help,..does some one still have a solution to my problem,...is my regex correct ?..please!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the answer to the question I asked?
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry.....i am having net connection problems here....load shedding in India !
well matcher.group(0) returns.... an empty string !
 
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
Originally posted by omkar patkar:
well matcher.group(0) returns.... an empty string !


Which means what? Based on your code, does this mean that your regular expression is matching an empty string?

BTW, I'm being rhetorical. Yes, you are matching an empty string. Now the questions is... How is your pattern matching an empty string?

Henry
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not really ! ..... in my regex, i have mentioned a group to be captured as :-
(.*?) just after a closing double quote and before a pipe symbol.

Therefore from my testString, it must capture: - "\n\nOmkar\n" atleast !

this was my expectation from the regex that i wrote! ...the matcher.group(1) is returning null and matcher.group(0) or matcher.group() is returning an empty string!

any solution ?
 
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
not really ! ..... in my regex, i have mentioned a group to be captured ....


Hint: Does the "|" have special meaning in a regex?

Henry
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes! ...i want my capture to stop just before pipe symbol !
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shall i post my complete program and the complete output ?
 
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
Originally posted by omkar patkar:
yes! ...i want my capture to stop just before pipe symbol !


Read my question again... Does a "|" have special meaning in a regex? Does it actually mean something else besides a "pipe symbol"?

Henry
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The intention of pipe symbol is just to help regex that it should stop capturing the moment it encounters this symbol....other wise, there is no other special meaning of the pipe symbol.

I think there is problem, in how "\n" is interpreted by regex engine....

for example...i hardcoded in my example.....something like this:-




Thus now my regex is :- "(.*?)|

and my test string becomes:- "\n\nOmkar\n|

the output is:-
The parameter value is :

and if check the matcher2 object it says:-
java.util.regex.Matcher[pattern="(.*?)| region=0,10 lastmatch="]

so i guess, the regex is not able to capture the "\n" ...i hope i am right ?

but then how do i capture ?
 
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
The intention of pipe symbol is just to help regex that it should stop capturing the moment it encounters this symbol....other wise, there is no other special meaning of the pipe symbol.....


Okay, I give up. No more hints...

In a regex, a "|" has special meaning. Maybe you don't have a special meaning for it, but a regex has a special meaning for it. And it does *not* mean the literal pipe symbol.

Take a look at the JavaDoc.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

Look up what a "|" means in a regex, and you will realize why it is matching a blank string.

Henry
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh nooo ! ....Thanks Man ! .....phew! i am so sorry henry....i just overlooked, pipe symbol...it never occured to me that it could have a special meaning....phew ! after you mentioned that pipe symbol has special it suddenly striked me and i changed the regex ...here is the change i made:-

Regex as :-
String parameterRegex = "name=\""+paramName+"\"[\n\r]+?(.+?)[\n\r]+?\\|";


i have escaped the pipe symbol now !....

Thank you so much ! ...now i am getting the intended result !

Thank you so much and sorry for being such a big problem for everyone!

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got there in the end. Well done. And you'll never forget | as long as you live!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!