【问题标题】:Regex with or and specific chars带有 or 和特定字符的正则表达式
【发布时间】:2022-11-18 22:55:54
【问题描述】:

我无法为特定规则创建正则表达式。

主要规则:

[FIX   -A or -D or -P with numbers and several times is possible     uppercase string]      :     string

我的测试:

[FIX-A2569] TOTO : toto // should passed
[FIX-D256458-P4565] TOTO : toto // should passed
[FIX-D123123-A123123-A123123-P123123] TOTO : toto // should passed
[FIX-D123123-A123123-P123123] TOTO : toto // should passed
[FIX-D123123-A123123-A123123-P123123] TOTO : toto // should passed
[FIX-D123123-A123123A123123-P123123] TOTO : toto // should not passed
[FIX-D123123A123123A123123P123123] TOTO : toto // should not passed
[FIX-D123123-A123123A123123P123123] TOTO : toto // should not passed

我的正则表达式:

/\[FIX-A[0-9]+|-D[0-9]+|-P[0-9]+\] [A-Z0-9 _]+ : .+/g

我的结果:

什么都没过去。

【问题讨论】:

  • 不要使用g标志,正则表达式可以如下所示,或^\[FIX(?:-[ADP]\d+)+] [A-Z0-9_]+ : .+

标签: regex


【解决方案1】:

只需保持您的正则表达式简短并使用工具从左到右设计它。这样您就可以看到表达式何时卡住。

我建议使用 /[FIX(-[ADP]d+)+] [A-Z0-9]+ : .*/ 作为正则表达式:

https://regexr.com/72nd9

使用 Regexr 的解释函数来了解有关正则表达式的更多信息。

【讨论】:

    猜你喜欢
    • 2011-02-20
    • 2011-10-16
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    相关资源
    最近更新 更多