• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with regular expression characters  RSS feed

 
Kajal Sharma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.
 
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 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
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot. This solution seems to work. Please explain how this code is actually working
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!