Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

edited tags
Link
VLAZ
  • 29.6k
  • 9
  • 65
  • 88
Remove design-patterns tag. RegEx pattern != design pattern.
Link
jaco0646
  • 17.4k
  • 10
  • 70
  • 100
added 22 characters in body
Source Link
Pshemo
  • 124.6k
  • 26
  • 194
  • 280

SCENARIO :

Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher(" WhiteSpace");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher(" WhiteSpace");

I am trying to get whitespaces at the beginning of a line. I want to get exact number of white spaces matcher true. My string is " WhiteSpace".
Problem is both matcher and matcher2 work on this string.

The thing I want is:
A pattern that only get 1 white space, but this pattern should not work for 2 white space string. In the scenario below both matcher.find() and matcher2.find() are true. But matcher.find() should be false, matcher2.find() should be true.

I want matcher to be true for :" WhiteSpace"" WhiteSpace",
I want matcher 2 to be true false for :" WhiteSpace",
" WhiteSpace" (two spaces)
I want matchermatcher2 to be falsetrue for :" WhiteSpace",

SCENARIO :" WhiteSpace".

Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher(" WhiteSpace");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher(" WhiteSpace");

The thing iI want to do is;

I have a string " two whitespaces"" two whitespaces".

Below both if statements are true. matchermatcher should be false. matcher2matcher2 should be true.

Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher("   twotwo whitespaces");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher("   twotwo whitespaces");
if(matcher.find==truefind()==true){
 //XXXXXXXXXXX
} else if(matcher2.find==truefind()==true){
 //YYYYYYYYYYY
}

Thanks,

I am trying to get whitespaces at the beginning of a line. I want to get exact number of white spaces matcher true. My string is " WhiteSpace".
Problem is both matcher and matcher2 work on this string.

The thing I want is:
A pattern that only get 1 white space, but this pattern should not work for 2 white space string. In the scenario below both matcher.find() and matcher2.find() are true. But matcher.find() should be false, matcher2.find() should be true.

I want matcher to be true for :" WhiteSpace",
I want matcher 2 to be true for :" WhiteSpace",
I want matcher to be false for :" WhiteSpace",

SCENARIO :

Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher(" WhiteSpace");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher(" WhiteSpace");

The thing i want to do is;
I have a string " two whitespaces".
Below both if statements are true. matcher should be false. matcher2 should be true.

Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher("   two whitespaces");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher("   two whitespaces");
if(matcher.find==true){
XXXXXXXXXXX
} else if(matcher2.find==true){
YYYYYYYYYYY
}

Thanks,

SCENARIO :

Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher(" WhiteSpace");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher(" WhiteSpace");

I am trying to get whitespaces at the beginning of a line. I want to get exact number of white spaces matcher true. My string is " WhiteSpace".
Problem is both matcher and matcher2 work on this string.

The thing I want is:
A pattern that only get 1 white space, but this pattern should not work for 2 white space string. In the scenario below both matcher.find() and matcher2.find() are true. But matcher.find() should be false, matcher2.find() should be true.

I want matcher to be true for" WhiteSpace", false for" WhiteSpace" (two spaces)
I want matcher2 to be true for :" WhiteSpace".


The thing I want to do is;
I have a string " two whitespaces".
Below both if statements are true. matcher should be false. matcher2 should be true.

Pattern whitespace = Pattern.compile("^\\s");
matcher = whitespace.matcher(" two whitespaces");
Pattern whitespace2 = Pattern.compile("^\\s\\s");
matcher2 = whitespace2.matcher(" two whitespaces");
if(matcher.find()==true){
 //XXXXXXXXXXX
} else if(matcher2.find()==true){
 //YYYYYYYYYYY
}
added 22 characters in body
Source Link
Pshemo
  • 124.6k
  • 26
  • 194
  • 280
Loading
added 20 characters in body
Source Link
brso05
  • 13.3k
  • 2
  • 25
  • 41
Loading
added 563 characters in body
Source Link
ivbtar
  • 877
  • 3
  • 14
  • 36
Loading
Source Link
ivbtar
  • 877
  • 3
  • 14
  • 36
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /