Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

regex without " at the end

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have escaped a double-qoute- \" at the end of the regex- might try removing it.
 
author
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the advices!
reply
    Bookmark Topic Watch Topic
  • New Topic