正则表达式有问题吗? [英] Regular expression problem?

查看:80
本文介绍了正则表达式有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为打印机对话框编写图案时遇到问题.它必须如下所示:
页数:1-6,9,22-28,31,38

它表示第1至6和9页以及第22至28页以及第31和38页
如何匹配该表达式?
在此先感谢.

I have a problem writing my pattern for printer dialog. It must be like below:
Pages: 1-6,9,22-28,31,38

It means pages 1 to 6 and 9 and pages from 22 to 28 and pages 31 and 38
How can I match this expression?
Thanks in advance.

推荐答案

看看下面的文章.

30分钟正则表达式教程

轻松学习正则表达式(RegEx)
Have a look at below articles.

The 30 Minute Regex Tutorial

Learn Regular Expressions (RegEx) with Ease


问题要匹配的模式是

1.个别页码,例如9,31
2.页面范围如1-6,22-28
各个页码和页码范围用,以任意顺序分隔.

为此,可以使用以下正则表达式

^\s*((\d+|\d+\s*-\s*\d+)\s*,\s*)*(\d+|\d+\s*-\s*\d+)\s*
As seen from the question the pattern to be matched is

1. Individual page number like 9,31
2. Page range like 1-6,22-28
Individual page numbers and Page range are separated by , in any order.

For this purpose the following regular expression can be used

^\s*((\d+|\d+\s*-\s*\d+)\s*,\s*)*(\d+|\d+\s*-\s*\d+)\s*




表达式^\s*((\d+|\d+\s*-\s*\d+)\s*,\s*)*的第一部分匹配单个页码或页面范围(-包含零个或多个空格),后跟一个,(包含零个或多个空格)
然后表达式的第二部分确保字符串以单个页码或页范围结束,结尾不带逗号

(\ d + | \ d + \ s *-\ s * \ d +)\ s *


The first part of expression ^\s*((\d+|\d+\s*-\s*\d+)\s*,\s*)* matches either a single page number or a page range (- surrounded by zero or more spaces) followed by a , (surrounded by zero or more spaces)
then the second part of expression ensures that string ends with either single page number or a page range with out a comma at the end

(\d+|\d+\s*-\s*\d+)\s*


这篇关于正则表达式有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆