我正在用正则表达式挣扎,该正则表达式可以从语句中提取类似指标的值。下面是一些示例中,我遇到过:使用正则表达式提取尺寸度量值
样题:
- 图像像素200×500像素模糊 - 提取200×500像素
- 图像像素200×500的模糊 - 提取200×500
- 100.22 X 200.55 X 90.55毫米是手机的尺寸 - 提取物100.22 x 200.55 x 90.55毫米
- 手机尺寸为100.22x200.55x90.55毫米。 - 提取100.22x200.55x90.55毫米
到目前为止我的代码如下
String str_array[] = new String[4];
\t \t str_array[0] = "Image pixel 200x500 px blur";
\t \t str_array[1] = "Image pixel 200 x 500 blurring";
\t \t str_array[2] = "100.22 x 200.55 x 90.55 mm is the size of the handphone";
\t \t str_array[3] = "The mobile phone is 100.22x200.55x90.55 mm in dimension.";
\t \t for (int i=0;i<str_array.length;i++){
\t \t \t Pattern pty_resolution_ratio_metrics_try = Pattern.compile("(\\d+)[\\.\\d]+(\\s*)x");
\t \t \t Matcher matcher_value_metrics_error_try = pty_resolution_ratio_metrics_try.matcher(str_array[i]);
\t \t \t while (matcher_value_metrics_error_try.find()) {
\t \t \t \t System.out.println("index: "+i+"-"+matcher_value_metrics_error_try.group(0));
\t \t \t }
\t \t }
从上述码的结果:
- 指数:0 -200x
- index:1-200 x
- 指数:2-100.22 X
- 指数:2-200.55 X
- 指数:3-100.22x
- 指数:3-200.55x
任何正则表达式的建议?需要帮助。
谢谢!
您不需要重新编译相同的模式。做一个静态的决赛。 – EJP