简体   繁体   English

Ruby正则表达式匹配模式后跟另一个模式

[英]Ruby regex to match a pattern followed by another pattern

I already have a regex to match only single digits in a comma-delimited string. 我已经有一个正则表达式 ,只匹配逗号分隔字符串中的单个数字。 I need to update it to match the strings like following: 我需要更新它以匹配以下字符串:

 5|5,4,3
 2|1,2 , 3

The constraints are 约束是

  • it should start with a single digit in range of 1-5, followed by a pipe character (|) 它应该以1-5的范围内的单个数字开头,后跟管道符(|)
  • the string followed by the pipe character - it should be a single digit in range of 1-7, optionally followed by a comma. 字符串后跟管道字符 - 它应该是1-7范围内的单个数字,可选地后跟逗号。 This pattern can be repetitive. 这种模式可以重复。 For eg following strings are considered to be valid, after the pipe character: 例如,在管道字符之后,以下字符串被认为是有效的:

     "6" "1,7" "1,2,3, 4,6" "1, 4,5,7" 

However following strings are considered to be invalid 但是,以下字符串被视为无效

    "8"

    "8, 9,10"

I tried with following (a other variations) 我尝试了以下(其他变化)

  \A[1-5]\|[1-7](?=(,|[1-7]))*

but it doesn't work as expected. 但它没有按预期工作。 For eg for sample string 例如用于样品串

  5|5,4, 3, 10,5

it just matches 它只是匹配

  5|5

I need to capture the digit before pipe character and all the matching digits followed by the pipe character. 我需要捕获管道字符前的数字和所有匹配的数字后跟管道字符。 For eg in following sample string 5|5,4, 3, 2, 1 the regex should capture 例如,在以下样本字符串5 | 5,4,5,2,1中,正则表达式应该捕获

  1. 5
  2. [5, 4, 3, 2, 1] [5,4,3,2,1]

Note: I am using Ruby 2.2.1 注意:我使用的是Ruby 2.2.1

Also do you mind letting me know what mistake I made in my regex pattern which was not making it work as expected? 另外你还介意让我知道我在我的正则表达式模式中犯了什么错误,这个错误没有按预期工作吗?

Thanks. 谢谢。

You could try the below regex. 你可以试试下面的正则表达式。

^([1-5])\|([1-7]\s*(?:,\s*[1-7])*)$

Example: 例:

> "5|5,4, 3, 2, 1".scan(/^([1-5])\|([1-7]\s*(?:,\s*[1-7])*)$/)
=> [["5", "5,4, 3, 2, 1"]]

OR 要么

> "5|5,4, 3, 2, 1".scan(/([1-5])\|([1-7] ?(?:, ?[1-7])*)$/)
=> [["5", "5,4, 3, 2, 1"]]

You can try the following regex that will match digits and a group of comma/space separated digits after a pipe: 您可以尝试以下正则表达式匹配数字和管道后的一组逗号/空格分隔数字:

^[1-5]\|(?:(?:[1-7]\s*,\s*)+\s*[1-7]?|[1-7])\b

Here is a demo . 这是一个演示

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

相关问题 如果后面没有模式,则正则表达式匹配 - regex match if not followed by pattern Python正则表达式匹配所有出现的十进制模式,后跟另一个模式 - Python Regex match all occurrences of decimal pattern followed by another pattern 如何获得模式的不匹配项:正则表达式后不跟另一个正则表达式 - How to get a no-match for a pattern: regex not followed by another regex 正则表达式匹配一个字面模式,后跟零个或多个字符,再跟另一个字面模式 - Regex to match a literal pattern followed by zero or more characters followed by another literal pattern RegEx:找到一个模式,然后是另一个带有间隙的模式 - RegEx: Finding a pattern followed by another pattern with a gap 如果模式后面跟着另一个模式,正则表达式应该会失败 - Regex should fail if pattern is followed by another pattern 正则表达式模式跳过一个模式并匹配另一个模式 - Regex pattern to skip a pattern and match another pattern 如何在正则表达式中匹配字符串后跟重复模式? - How to match string followed by repeated pattern in regex? 正则表达式不匹配模式,后跟字符串中的水平省略号 - Regex not match pattern followed by horizontal ellipsis in string 正则表达式匹配字符串的特定模式,后跟数字 - regex to match specific pattern of string followed by digits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM