【问题标题】:Regex for particular pattern with spaces带有空格的特定模式的正则表达式
【发布时间】:2016-09-28 04:51:55
【问题描述】:

我需要检查的模式是“1w 1d 1h 1m 1s”,有一些限制,例如每个部分只能有 1 到 3 个数字,可以缺少任何部分,可以有任何数字或空格和零件之间的其他空白字符,零件必须按顺序排列。我为它写了一个基本的正则表达式:

^((?:\s*\d{1,3}[wW]\s*)?(?:\s+\d{1,3}[dD]\s*)?(?:\s+\d{1,3}[hH]\s*)?(?:\s+\d{1,3}[mM]\s*)?(?:\s+\d{1,3}[sS]\s*)?)$

现在的问题 - 我卡住了,因为如果我写“1d 1h” - 它不会通过,因为它需要在“1d”之前有一个空格,但我不能没有那个空格,“1w1d”应该是不正确的。使用正则表达式条件并不是一个真正的选择。

【问题讨论】:

  • 这可以帮助 ^((?:\s*\d{1,3}[wW]\s+)?(?:\s*\d{1,3}[dD]\ s+)?(?:\s*\d{1,3}[hH]\s+)?(?:\s*\d{1,3}[mM]\s+)?(?:\s*\ d{1,3}[sS]\s*)?)$
  • 这样,case "1w 1d" 将无效,因为它现在需要在 w, d, h 之后 1 个空格。
  • 试试这个 ^((?:\s*\d{1,3}[wW]\s+|\s*\d{1,3}[wW]\s*$) ?(?:\s*\d{1,3}[dD]\s+|\s*\d{1,3}[dD]\s*$)?(?:\s*\d{1, 3}[hH]\s+|\s*\d{1,3}[hH]\s*$)?(?:\s*\d{1,3}[mM]\s+)?(?: \s*\d{1,3}[sS]\s*|\s*\d{1,3}[sS]\s*$)?)
  • 1d1h 是有效输入吗?
  • @SamjithDasan 谢谢,这个确实有效 :)

标签: regex actionscript-3


【解决方案1】:
^((?:\s*\d{1,3}[wW]\s+|\s*\d{1,3}[wW]\s*$)?(?:\s*\d{1,3}[dD]‌​\s+|\s*\d{1,3}[dD]\s‌​*$)?(?:\s*\d{1,3}[hH‌​]\s+|\s*\d{1,3}[hH]\‌​s*$)?(?:\s*\d{1,3}[m‌​M]\s+)?(?:\s*\d{1,3}‌​[sS]\s*|\s*\d{1,3}[s‌​S]\s*$)?)$

【讨论】:

  • 嗯,这个模式是正确的,但它也看到任何字符串的“有效”结果(空的有效结果)。您应该像我一样在最后一个括号后添加“$”,这样只有真正的空字符串才有效,而不是每个字符串。
  • 哦!是的,添加了。
【解决方案2】:

除了@SamjithDasan 正确的provided,您还可以使用ActionScript-3 支持的正则表达式conditionals,如果它是一个选项:

var regex: RegExp =
/^(\s*\d{1,3}[wW]\s*(?(?=.)\s+|))?(\d{1,3}[dD]\s*(?(?=.)\s+|))?(\d{1,3}[hH]\s*(?(?=.)\s+|))?(\d{1,3}[mM]\s*(?(?=.)\s+|))?(\d{1,3}[sS]\s*)?$/g;


为了测试它们,下面会生成所有可能的“真”组合,并针对每个组合检查正则表达式:
var i:uint = 1;
for each(var w: String in ["1w ", ""]) {
  for each(var d: String in ["1d ", ""]) {
    for each(var h: String in ["1h ", ""]) {
      for each(var m: String in ["1m ", ""]) {
        for each(var s: String in ["1s ", ""]) {
          var str:String = w+d+h+m+s;
          var desc:String = strFill(i++ + ". str=\""+str+"\"");
          trace(desc+" RegEx-matched: "+(str.match(regex).length == 1 ? "yes" : "no"));
        }
      }
    }
  }
}
function strFill(str:String, w:uint = 25, fill:String="."):String {
   for (var i:uint=str.length; i<=w; i++) str+=fill; return str;
}

输出是:

1. str="1w 1d 1h 1m 1s ".. RegEx-matched: yes
2. str="1w 1d 1h 1m "..... RegEx-matched: yes
3. str="1w 1d 1h 1s "..... RegEx-matched: yes
4. str="1w 1d 1h "........ RegEx-matched: yes
5. str="1w 1d 1m 1s "..... RegEx-matched: yes
6. str="1w 1d 1m "........ RegEx-matched: yes
7. str="1w 1d 1s "........ RegEx-matched: yes
8. str="1w 1d "........... RegEx-matched: yes
9. str="1w 1h 1m 1s "..... RegEx-matched: yes
10. str="1w 1h 1m "....... RegEx-matched: yes
11. str="1w 1h 1s "....... RegEx-matched: yes
12. str="1w 1h ".......... RegEx-matched: yes
13. str="1w 1m 1s "....... RegEx-matched: yes
14. str="1w 1m ".......... RegEx-matched: yes
15. str="1w 1s ".......... RegEx-matched: yes
16. str="1w "............. RegEx-matched: yes
17. str="1d 1h 1m 1s ".... RegEx-matched: yes
18. str="1d 1h 1m "....... RegEx-matched: yes
19. str="1d 1h 1s "....... RegEx-matched: yes
20. str="1d 1h ".......... RegEx-matched: yes
21. str="1d 1m 1s "....... RegEx-matched: yes
22. str="1d 1m ".......... RegEx-matched: yes
23. str="1d 1s ".......... RegEx-matched: yes
24. str="1d "............. RegEx-matched: yes
25. str="1h 1m 1s "....... RegEx-matched: yes
26. str="1h 1m ".......... RegEx-matched: yes
27. str="1h 1s ".......... RegEx-matched: yes
28. str="1h "............. RegEx-matched: yes
29. str="1m 1s ".......... RegEx-matched: yes
30. str="1m "............. RegEx-matched: yes
31. str="1s "............. RegEx-matched: yes
32. str=""................ RegEx-matched: no

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2011-10-16
    • 1970-01-01
    相关资源
    最近更新 更多