Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,859 questions

51,780 answers

573 users

How to use \S regular expression (REGEX) (Any non-whitespace character) in Java

1 Answer

0 votes
import java.util.regex.Matcher;
import java.util.regex.Pattern;
     
public class Example {
    private static void searchREGEX(String regex, String... search) {
        Pattern pattern = Pattern.compile(regex);
         
        for (String input : search) {
            Matcher matcher = pattern.matcher(input);
    
            boolean found = false;
             
            while (matcher.find()) {
               System.out.println(String.format("Found" + " \"%s\" " + "from index %d to %d",
                                            matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
             
            System.out.println();
             
            if (!found) {
                 System.out.println(String.format("%s: \"%s\" Not found\n", regex, input));
            }
        }
    }  
    public static void main(String[] args) {
        // \S   (A non-whitespace character: [^\s])
        searchREGEX("\\S", " ", "1", "f35\\n", ".", "0\\r12", "\t");
    }
     
}
     
     
     
/*
run:
      
\S: " " Not found
 
Found "1" from index 0 to 1
 
Found "f" from index 0 to 1
Found "3" from index 1 to 2
Found "5" from index 2 to 3
Found "\" from index 3 to 4
Found "n" from index 4 to 5
 
Found "." from index 0 to 1
 
Found "0" from index 0 to 1
Found "\" from index 1 to 2
Found "r" from index 2 to 3
Found "1" from index 3 to 4
Found "2" from index 4 to 5
 
 
\S: "   " Not found
      
*/

 



answered Jan 22, 2016 by avibootz
edited May 14, 2024 by avibootz

Related questions

1 answer 114 views
1 answer 211 views
2 answers 144 views
1 answer 175 views
3 answers 212 views
...