数字+百分号+off用java中的正则表达式怎么表示啊?如25% off,23.5% off等等,百分数和off之间有空格哦?

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/15 04:30:35
数字+百分号+off用java中的正则表达式怎么表示啊?如25% off,23.5% off等等,百分数和off之间有空格哦?

数字+百分号+off用java中的正则表达式怎么表示啊?如25% off,23.5% off等等,百分数和off之间有空格哦?
数字+百分号+off用java中的正则表达式怎么表示啊?如25% off,23.5% off等等,百分数和off之间有空格哦?

数字+百分号+off用java中的正则表达式怎么表示啊?如25% off,23.5% off等等,百分数和off之间有空格哦?
/**
* @author eatonfang
* @version 1.0
*
*/
public class Test {
\x05/**
\x05 * @param args
\x05 */
\x05public static void main(String[] args) {
\x05\x05// test data
\x05\x05String str1 = "a2% off";
\x05\x05String str2 = "123% off";
\x05\x05String str3 = "23% off";
\x05\x05String str4 = "23%off";
\x05\x05String str5 = "23* off";
\x05\x05String str6 = "23% off";
\x05\x05String str7 = "23% of";
\x05\x05String str8 = "% off";
\x05\x05String str9 = "23%\x05off";
\x05\x05// regex
\x05\x05String regex = "\\d+% off";
\x05\x05// matches
\x05\x05System.out.println(str1.matches(regex));
\x05\x05System.out.println(str2.matches(regex));
\x05\x05System.out.println(str3.matches(regex));
\x05\x05System.out.println(str4.matches(regex));
\x05\x05System.out.println(str5.matches(regex));
\x05\x05System.out.println(str6.matches(regex));
\x05\x05System.out.println(str7.matches(regex));
\x05\x05System.out.println(str8.matches(regex));
\x05\x05System.out.println(str9.matches(regex));
\x05}
}
运行结果:
false
true
true
false
false
false
false
false
false