Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

????? - matches one or more nonblank characters

 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using 1.4 related String Matches method,
I know (Learned from Dirk Schreckmann on this forum ;-)
--
"." - matches a single character
".+" - matches one or more characters
".*" - matches zero or more characters
Can someone tell me what do i write if I want to say any character other than blank space ?

I mean
??? - matches one or more nonblank characters.
I tried,
[a-zA-Z_0-9]+
[^\s] --- this gives me error related to escape character.
so I changed it to
[^\\s] but still does not work.
Also I tried,
: [^\S] s capital....
but no use.
Please help me.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try "\\S". You don't need the braces in combination with predefined character classes like \S. And whenever you write a String literal representing a pattern you need to double each \.
Note that I also don't see anything wrong with "[^\\s]" other than the fact that it's longer than necessary. Are you sure it didn't work?
[ August 13, 2002: Message edited by: Jim Yingst ]
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String S = "abc or xyz pqr";
sub1 = S.subString(3,5)
Pattern p = Pattern.compile("^\\s");
Matcher m = p.matcher(Sub1);
if (m.matches()))
System.out.println("string contains atleast one character otherthan space");
i dont get this o/p ??
Am I wrong somewhere?
I want to check if I have any other character other than space between first space after C from abc and O of Or.
Please correct me if I am going wrong?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will probably need to be able to compile your code before you can expect to run it and get meaningful output. Then, note that the matches() method is looking for a match of the entire string at once, not just a substring. (This is contrary to what I originally told you here, where I was thinking of something else.) Try using find() instead of matches(). Also, there is a critical difference between "[^\\s]" and "^\\s", as the ^ character only means "exclude" if it's inside brackets. So "^\\s" means look for a ^ followed by a whitespace, while "[^\\s]" means look for any char except a whilespace. (And don't forget to try the "\\S" as I suggested.)
[ August 13, 2002: Message edited by: Jim Yingst ]
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Thank u for taking time to look into my problem.
I am definitely compiling the code after changing it and before runnning.
I tried ,
"\\S"
"\\s"
"[^\\s]"
but does not enter the IF condition to print the statement.
Can u please suggest me what other way I can achieve this.
What "find" instead of match are u refering to?
There is no method called "find" in String?
Thanks!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last code sample you gave does not compile. I have no idea what code you're actually running. The find() method I refer to is in the Matcher class.
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String S = "abc or xyz pqr";
String sub1 = S.substring(3,5);
Pattern p = Pattern.compile("\\S");
Matcher m = p.matcher(sub1);
if (m.matches())
System.out.println("string contains atleast one character otherthan space");

There were some syntax errors as I had to change the code a bit inoder to remove unwanted complications while posting it here.
This is the code which compiles correctly..
Sorry for the syntx errors...
can u change the code now in order to run it successfully?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that's better. From previous post:
Try using find() instead of matches().

[ August 13, 2002: Message edited by: Jim Yingst ]
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes It worked perfect!!
Thanks a lot Jim!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic