我需要一个正则表达式来查找包含这些单词的数字:
1st, 2nd, 3rd, 4th, 5th.
从以下文本中:
<xps:span class="ref_sn">Huang</xps:span></xps:span> <xps:span
class="ref_au"><xps:span class="ref_gn">K.</xps:span> <xps:span
class="ref_sn">Chingin</xps:span></xps:span> <xps:span
class="ref_au"><xps:span class="ref_gn">R.</xps:span> <xps:span
class="ref_sn">Zenobi</xps:span> 1st</xps:span> <xps:span
class="ref_atitle">Real<span class='xps_ndash'>–iou</span>time,
on<span class='xps_ndash'> 2nd –iou</span>line 4th monitoring of
organic chemical reactions using 3rd extractive electrospray
ionization tandem mass 5th spectrometry</xps:span> <xps:span
class="ref_jtitle">Rapid Commun. Mass Spectrom.</xps:span>
我需要将这些字母表转换为sup。
我正在使用这个正则表达式,但它不起作用。
(\b)(\d+([st|nd|rd|th]+)\b)
发布于 2014-08-12 16:59:00
正则表达式也称为字符集,您可以告诉正则表达式引擎只匹配几个字符中的一个。
[st|nd|rd|th] any character of:
's', 't', '|', 'n', 'd',
'|', 'r', 'd', '|', 't', 'h'
您需要使用(...)
而不是[...]
你可以试试
\d+(?=st|nd|rd|th)
这是demo
示例代码:
String str = "1st, 2nd, 3rd, 4th, 5th.";
Pattern p = Pattern.compile("\\d+(?=st|nd|rd|th)");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group());
}
输出
1
2
3
4
5
您可以使用捕获组修改您的正则表达式,如下所示,并获得所需的匹配组:
Pattern p=Pattern.compile("(\\d+)(st|nd|rd|th)");
Matcher m=p.matcher(str);
while(m.find()){
System.out.println(m.group(1));
}
发布于 2014-08-12 16:58:59
只需要尝试一下:
只需尝试使用以下正则表达式:
(\d+(?:st|nd|rd|th))
demo
发布于 2014-08-12 17:03:00
只是稍微修改一下你的代码:
public static void main(String[] args) {
String s = "Huang K. Chingin R. Zenobi 1st Real–ioutime, on 2nd –iouline 4th monitoring of organic chemical reactions using 3rd extractive electrospray ionization tandem mass 5th spectrometry Rapid Commun. Mass Spectrom";
Pattern p = Pattern.compile("\\d+(?=st|nd|rd|th)");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
}
O/P:
1
2
4
3
5
https://stackoverflow.com/questions/25260093
复制相似问题