繁体   English   中英

如何使用正则表达式匹配此模式

[英]how to match this pattern using regex

我是 ruby​​ 的新手并尝试使用正则表达式。 基本上我想读取一个文件并检查它是否具有正确的格式。

Requirements to be in the correct format:
1: The word should start with from
2: There should be one space and only one space is allowed, unless there is a comma 
3: Not consecutive commas
4: from and to are numbers
5: from and to must contain a colon

from: z to: 2
from: 1 to: 3,4
from: 2 to: 3
from:3 to: 5 
from: 4 to: 5
from: 4 to: 7
to: 7 from: 6 
from: 7 to: 5
0: 7 to: 5
from: 24 to: 5
from: 7 to: ,,,5
from: 8 to: 5,,5
from: 9 to: ,5

如果我有正确的正则表达式,那么输出应该是:

from: 1 to: 3,4
from: 2 to: 3
from: 4 to: 5
from: 4 to: 7
from: 7 to: 5
from: 24 to: 5

所以在这种情况下,这些是错误的:

from: z to: 2     # because starts with z
from:3 to: 5      # because there is no space after from:
to: 7 from: 6     # because it starts with to but supposed to start with from
0: 7 to: 5        # starts with 0 instead of from
from: 7 to: ,,,5  # because there are two consecutive commas
from: 8 to: 5,,5  # two consecutive commas
from: 9 to: ,5    # start with comma

好的,你想要的正则表达式是这样的:

from: \d+(?:,\d+)* to: \d+(?:,\d+)*

这假设在from:列中也允许多个数字。 如果没有,你想要这个:

from: \d+ to: \d+(?:,\d+)*

要验证整个文件是否有效(假设它包含的所有行都是这样的),您可以使用这样的函数:

def validFile(filename)
    File.open(filename).each do |line|
        return false if (!/\d+(?:,\d+)* to: \d+(?:,\d+)*/.match(line))
    end
    return true
end

您正在寻找的称为负前瞻。 具体来说, \\d+(?!,,)表示:匹配 1 个或多个连续数字,后面不跟 2 个逗号。 这是整个事情:

str = "from: z to: 2
from: 1 to: 3,4
from: 2 to: 3
from:3 to: 5 
from: 4 to: 5
from: 4 to: 7
to: 7 from: 6 
from: 7 to: 5
0: 7 to: 5
from: 24 to: 5
from: 7 to: ,,,5
from: 8 to: 5,,5
from: 9 to: ,5
"

str.each_line do |line|
  puts(line) if line =~ /\Afrom: \d+ to: \d+(?!,,)/
end

输出:

from: 1 to: 3,4
from: 2 to: 3
from: 4 to: 5
from: 4 to: 7
from: 7 to: 5
from: 24 to: 5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM