• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problem with regular expression characters

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am using following code to to find out the string which has all duplicate white spaces in it. Xml is the input.



This stops working when in the xml I have a line written as:


The space before \n is causing this problem. Because when I remove this space it works fine.

Please help.
 
author
Posts: 23958
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
The regex is designed to look for one or more whitespace characters, in between two quote characters. What is this duplication check you are referring to?

Henry
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prefix the regular expression with "(?s)". Check the Javadoc for java.util.regex.Pattern for details.
 
Kajal Sharma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The complete string which I have in the xml is :



I want to keep the last portion intact.

global.displayString = global.title+" \n" + "$" + global.price + " ";

But \n is causing a problem.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I say again - prefix the regular expression with "(?s)". Check the Javadoc for java.util.regex.Pattern for details.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I'm probably wrong about the (?s). Could you explain why you have '\"' in your regex because it is not obvious to me that you have a '"' before the ' ' you are trying to match?
[ November 03, 2008: Message edited by: James Sabre ]
 
Kajal Sharma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading data from the xml file. So in the text mentioned above. In the end there is string of empty characters. I am searching for the complete string i.e quotes(")with white spaces.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to compress multiple spaces within quotes down to a single space? You can't use \s for that because it matches all ASCII whitespace characters, which includes linefeeds ("\n"). Try this:
 
Kajal Sharma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. This solution seems to work. Please explain how this code is actually working
 
Marshal
Posts: 80214
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lots more about regular expressions here in the Java Tutorials. There is explanation about {2,} somewhere, probably in the "quantifiers" section.
 
reply
    Bookmark Topic Watch Topic
  • New Topic