DEV Community

Ayush Shalya
Ayush Shalya

Posted on

Regex Whitespace 4. How to match whitespace and non whitespace characters.

The \s expression any whitespace characters[space,tab] and \S
expression matches every non whitespace characters.
e.g :- String Pattern :- \\S\\s\\S\\s\\S
Test String :- a b c def

Note:- Use \\ instead of \ in Java.

Sample code in Java:

import java.io.;
import java.util.Scanner;
import java.util.regex.
;
class RegexSearch{
public void tester(String Regex_Pattern){
Scanner sc= new Scanner(System.in);
String Test_String = sc.nextLine();
Pattern p = Pattern.compile(Regex_Pattern);
Matcher m = p.matcher(Test_String);
while(m.find()){
System.out.print(m.group());
}
}
}
public class Main{
public static void main(String[]args){
RegexSearch obj = new RegexSearch();
obj.tester("\\S\\s\\S\\s\\S");
}
}

You can view the code here also.

Do read my previous blog on Regex.

                  👍 Have a nice day, Coders.👍
Enter fullscreen mode Exit fullscreen mode

Top comments (0)