Java 如何使用正则表达式匹配表达式,如b1、b2、。。。通过b15,但不进一步?
我在运行MacOs Mojave的Mac上使用Java、Eclipse。 看起来这很容易,但我已经花了4-5个小时。 需要识别以下字符串:Java 如何使用正则表达式匹配表达式,如b1、b2、。。。通过b15,但不进一步?,java,regex,eclipse,Java,Regex,Eclipse,我在运行MacOs Mojave的Mac上使用Java、Eclipse。 看起来这很容易,但我已经花了4-5个小时。 需要识别以下字符串:b1、b2、b3。。。b14、b15 已尝试“^b1[012345]{1}$|^b[1-9]?$” 和其他: (^b1[012345]{1}$)|(^b[1-9]{1}$) b(1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15){1}$ 甚至 ^b('1''2''3''4''5''6
b1、b2、b3。。。b14、b15
已尝试“
^b1[012345]{1}$|^b[1-9]?$”
和其他:
(^b1[012345]{1}$)|(^b[1-9]{1}$)
b(1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15){1}$
甚至
^b('1''2''3''4''5''6''7'.'15'){1}$
非常感谢。试试这个正则表达式:
\bb(?:1[0-5]|[1-9])\b
在JAVA中,您需要用另一个\
转义\
说明:
-匹配单词边界\b
-匹配字母b
b
-匹配1,后跟范围(?:1[0-5]|\d)
内的数字,或匹配0-5
1-9
-匹配单词边界\b
\bb(?:1[0-5]|[1-9])\b
在JAVA中,您需要用另一个\
转义\
说明:
-匹配单词边界\b
-匹配字母b
b
-匹配1,后跟范围(?:1[0-5]|\d)
内的数字,或匹配0-5
1-9
-匹配单词边界\b
- 谢谢。但它不起作用;我摆弄它,无法投入工作。下面的内容很详细,但它起作用了:
String first = pageText.substring(0, 1);
String rest = pageText.substring(1, pageText.length());
String pattern = "[^0-9]";
Matcher matcher = Pattern.compile(pattern).matcher(rest);
while (matcher.find()) {
JOptionPane.showMessageDialog(null,
"<html>Only b followed by a number between 1 and “
+ “15 in the page number field",
"Page Number Is Not Recognizable",
JOptionPane.ERROR_MESSAGE);
return;
}
int intRest = Integer.parseInt(rest); // string to integer
if ((intRest > 0 && intRest < 16) && (first.equals("b"))) {
// valid
String first=pageText.substring(0,1);
字符串rest=pageText.substring(1,pageText.length());
字符串模式=“^0-9]”;
Matcher-Matcher=Pattern.compile(Pattern.Matcher)(rest);
while(matcher.find()){
JOptionPane.showMessageDialog(null,
“仅b后跟一个介于1和之间的数字”
+“页码字段中的15”,
“无法识别页码”,
JOptionPane.ERROR\u消息);
返回;
}
int intRest=Integer.parseInt(rest);//字符串到整数
如果((intRest>0&&intRest<16)和(&&first.equals(“b”)){
//有效的
谢谢。但它不起作用;我摆弄它,无法投入工作。下面的内容很详细,但它起作用了:
String first = pageText.substring(0, 1);
String rest = pageText.substring(1, pageText.length());
String pattern = "[^0-9]";
Matcher matcher = Pattern.compile(pattern).matcher(rest);
while (matcher.find()) {
JOptionPane.showMessageDialog(null,
"<html>Only b followed by a number between 1 and “
+ “15 in the page number field",
"Page Number Is Not Recognizable",
JOptionPane.ERROR_MESSAGE);
return;
}
int intRest = Integer.parseInt(rest); // string to integer
if ((intRest > 0 && intRest < 16) && (first.equals("b"))) {
// valid
String first=pageText.substring(0,1);
字符串rest=pageText.substring(1,pageText.length());
字符串模式=“^0-9]”;
Matcher-Matcher=Pattern.compile(Pattern.Matcher)(rest);
while(matcher.find()){
JOptionPane.showMessageDialog(null,
“仅b后跟一个介于1和之间的数字”
+“页码字段中的15”,
“无法识别页码”,
JOptionPane.ERROR\u消息);
返回;
}
int intRest=Integer.parseInt(rest);//字符串到整数
如果((intRest>0&&intRest<16)和(&&first.equals(“b”)){
//有效的