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,907 questions

51,839 answers

573 users

How to use regular expression (REGEX) to find text in string with Java

3 Answers

0 votes
package javaapplication1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        
        searchREGEX("DirectX 11 Game Programming", "Game");
        searchREGEX("DirectX 11 Game Programming", "11");
    }
   	
    private static void searchREGEX(String s, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);

        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;
        }
        if (!found) 
            System.out.println("Not found");
    }
}


/*
run:
 
Found "Game" from index 11 to 15
Found "11" from index 8 to 10
 
*/

 



answered Jan 17, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        
        searchREGEX("DirectX 11 Game Programming with Direct3D", "rect");
    }
   	
    private static void searchREGEX(String s, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);

        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;
        }
        if (!found) 
            System.out.println("Not found");
    }
}


/*
run:
 
Found "rect" from index 2 to 6
Found "rect" from index 35 to 39
 
*/

 



answered Jan 17, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        
        searchREGEX("DirectX 11 Game Programming with Direct3D", "m");
    }
   	
    private static void searchREGEX(String s, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);

        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;
        }
        if (!found) 
            System.out.println("Not found");
    }
}


/*
run:
 
Found "m" from index 13 to 14
Found "m" from index 22 to 23
Found "m" from index 23 to 24
 
*/

 



answered Jan 17, 2016 by avibootz

Related questions

1 answer 114 views
1 answer 176 views
1 answer 159 views
1 answer 213 views
1 answer 247 views
1 answer 171 views
...