• Post Reply Bookmark Topic Watch Topic
  • New Topic

regex without " at the end  RSS feed

 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output of my program is
myUrl=hw%2F45001001-45001032"
How to modify my regex so that the output will not has " at the end?
i.e. the desired output is:
myUrl=hw%2F45001001-45001032

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have escaped a double-qoute- \" at the end of the regex- might try removing it.
 
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
Mohamed Sanaulla wrote:You have escaped a double-qoute- \" at the end of the regex- might try removing it.


I don't think that will work -- as the extra quote was probably added to force the reluctant match to go to the end of quote. To get rid of the quote, simply create a sub-group that doesn't include the quote, and use that group. A second option is to put the quote into a look-ahead.

Henry
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mohamed Sanaulla wrote:You have escaped a double-qoute- \" at the end of the regex- might try removing it.


I don't think that will work -- as the extra quote was probably added to force the reluctant match to go to the end of quote.

Henry


Ah, yeah thats correct. Thanks Henry for the correction.
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mohamed Sanaulla wrote:You have escaped a double-qoute- \" at the end of the regex- might try removing it.


I don't think that will work -- as the extra quote was probably added to force the reluctant match to go to the end of quote. To get rid of the quote, simply create a sub-group that doesn't include the quote, and use that group. A second option is to put the quote into a look-ahead.

Henry


How to create a sub-group that doesn't include the quote?

still has the output
myUrl=hw%2F45001001-45001032"

How to put the quote into a look-ahead?

has empty (no) output.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The group you showed is correct, but then instead of printing the whole match, just print the match for the group -- i.e., m.group(1) will not include the quote.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That non-capturing group appears to strangely include elements of bot a positive and negative lookahead and would match !" as a positive lookahead.

Just my opinion, but for this use case I would match up to the last non-quote character following myUrl=hw%.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As EFH mentioned, your sub-group was done correctly -- your later code just needs to use the subgroup (in this case, group 1) instead.

albert kao wrote:
How to put the quote into a look-ahead?

has empty (no) output.


You changed the behavior of the regex. For some reason, you chose to use the negative look-ahead instead of the positive one. You actually want the quote to be searched for, so it should be the positive look-ahead.

Henry
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the advices!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!