编写一个通用的正则表达式,它可以从给定的句子中提取()中给出的以下模式:
a) Bridgestone Duravis M700 HD Tire LT265/75R16/10
(Extract : LT265/75R16/10)
b) Michelin Energy MXV4 S8 Tire P215/55R17 93V (
(Extract : P215/55R17 93V)
c) Bridgestone BLIZZAK DM-V2 Tire 235/55R18 (100T) (Extract : 235/55R18 )
d) Goodyear Eagle F1 Asymmetric A/S 215/45ZR17/XL Tire 91W (Extract : 215/45ZR17/XL)
e) Bridgestone Blizzak DM-V1 Tire 275/60R18 113R BW (Extract : 275/60R18 113R BW)
f) Continental ExtremeWinterContact Tire LT285/70R17/8 118Q BW (Extract : LT285/70R17/8 118Q BW)
g) MASTERCRAFT 285/70R17/E COURSER CXT OL TIRE (Extract : 285/70R17/E )
h) Pirelli Night Dragon Motorcycle Front Tire 130/90-16 2211500 (Extract : 130/90-16 )
答案 0 :(得分:1)
试试这个:
\w*\d\w*\/\w+(?:[\/-]\w+)?(?:\s(?:\d+[A-Z]\w*|[A-Z]{1,2})\b)*
答案 1 :(得分:1)
//Use below code to achieve the required objective:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
public class Extract {
//------------MAIN METHOD---------------------
public static void main(String args[]){
System.out.println("Extracted text ( ) : \n ");
try{
File file = new File("C:/Users/Manjunath/Desktop/himanshu.txt");
String absolutePath = file.getAbsolutePath();
FileReader fileReader = new FileReader(absolutePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while((line = bufferedReader.readLine())!= null)
{
stringBuffer.append(line);
stringBuffer.append("\n");
String mydata = line;
Pattern pattern = Pattern.compile("\\((.*?)\\)");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
System.out.println(matcher.group(1));
}
}
fileReader.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
//Regards,
From Manjunath
答案 2 :(得分:1)
//Use below code to achieve the required objective:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
public class Extract {
//------------MAIN METHOD---------------------
public static void main(String args[]){
System.out.println("Extracted text ( ) : \n ");
try{
File file = new File("C:/Users/Manjunath/Desktop/himanshu.txt");
String absolutePath = file.getAbsolutePath();
FileReader fileReader = new FileReader(absolutePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while((line = bufferedReader.readLine())!= null)
{
stringBuffer.append(line);
stringBuffer.append("\n");
String mydata = line;
Pattern pattern = Pattern.compile("\\((.*?)\\)");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
System.out.println(matcher.group(1));
}
}
fileReader.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
//Regards,
From Manjunath