I want to find a string (formed by concatenating a string and a regular expression) in another string. If the first line is in the second line, I want to get the start and end addresses of the matching phrase. In the following code, I want to find "baby accessories in India" in "baby_NN accessories_NNS India_NNP is_VBZ an_DT online_JJ shopping_NN portal_NN" and you want to get "baby_NN accessories_NNS India_NNP".
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatching {
public static void main(String aaa[])throws IOException
{
String line="baby accessories India";
String []str=line.split("\\ ");
String temp="";
int i,j;
j=0;
String regEx1 = "([A-Z]+)[$]?";
for(i=0;i<str.length;i++)
temp=temp+str[i]+"_"+regEx1+" ";
String para2="baby_NN accessories_NNS India_NNP is_VBZ an_DT online_JJ shopping_NN portal_NN ";
Pattern pattern1 = Pattern.compile(temp);
Matcher matcher1 = pattern1.matcher(para2);
if (para2.matches(temp)) {
i = matcher1.start();
j = matcher1.end();
String temp1=para2.substring(i,j);
System.out.println(temp1);
}
else {
System.out.println("Error");
}
}
}
source
share