开发者

Java regex to get part number

I have HTML that I need to extract a part number from, the HTML looks like:

javascript:selectItem('ABC123          1', '.....

I need to get the ABC123 from the above.

My code 开发者_运维知识库snippet:

Patterp p = Pattern.Compile("?????");
Matcher m = p.matcher(html);

if(m.find())
  partNumber = m.group(1).trim();

BTW, in the pattern, how do I escape for the character (

I now for quotes I do \"

thanks allot!


You escape ( by putting a \ before it. Because it's in a String, you need to escape the \ so the sequence is \\(. This should parse that snippet:

Pattern p = Pattern.compile("javascript:selectItem\\('(\\w+)");
Matcher m = p.matcher(html);
if (m.find()) {
  String partNumber = m.group(1);
}

I've assumed the part number is one or more word characters (meaning digits, letters or underscore).


You could use this:

Pattern regex = Pattern.compile("(?<=selectItem\\(')\\S*",Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
    ResultString = regexMatcher.group(1);
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜