简体   繁体   中英

Searching a string using regular expressions in java

I wonder how to retrieve more similar patterns present in a string array no matter the string length and how many such similar patterns existed..

For example:

Harry James Potter also known as Mr.Potter . Potter is very famous in hagwards. Harry James Potter also called Mr.Potter.

I need to find the contents between Harry James Potter and Mr.Potter:

The output should be

  1. also known as
  2. also called

Can any one help me out?

here is my code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexTestHarness {
    public static void main(String[] args){

        String regex = "Harry James Potter (.*?) Mr.Potter";

        String strToSearch = "Harry James Potter also known as Mr.Potter. Harry       James Potter is famous as  Mr. Potter";

        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

        Matcher matcher = pattern.matcher(strToSearch);

        while (matcher.find()) {

            System.out.println("Text is at "+matcher.group()+"::"+matcher.start()+"::     "+matcher.end());
            System.out.println(matcher.groupCount());

            System.out.println(matcher.group(1));
        }
    }
}

This regex will pick up anything contained between "Harry James Potter" and "Mr.Potter":

Harry James Potter (.*?) Mr\.Potter

Tested here

Depending on your implementation of Regex, you may need to retrieve result group 1.

Make sure you escape the period in Mr.Potter when writing your regex string. Also your strToSearch had random spaces in it which would make your regex not find what you are after. This code produces the example that you provided.

try {
        String regex = "Harry James Potter (.*?) Mr\\.Potter";
        String strToSearch = "Harry James Potter also known as Mr.Potter. Potter is very famous in hagwards. Harry James Potter also called Mr.Potter.";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(strToSearch);
        int start = 0;
        int count = 1;
        while (matcher.find(start)) {
            System.out.println(count + ". " + matcher.group(1));
            start = matcher.end();
            count++;
        }

    } catch(Exception ex) {
        ex.printStackTrace();
    }
    String s = "Harry James Potter also known as Mr.Potter . Potter is very famous in hagwards. Harry James Potter also called Mr.Potter.";
    Pattern pattern = Pattern.compile("(?<=Harry James Potter )(.*?)(?= Mr.Potter)");
    Matcher matcher = pattern.matcher(s);
    while (matcher.find()) {            
        System.out.println(matcher.group(1));
    }

output :

also known as
also called

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM