JAVA Regex因某种模式而被绞死

时间:2013-06-06 07:03:02

标签: regex

每当我尝试匹配正则表达式模式

    PREREQasdasdasdasdsadNot+es3adadaK2+0000000+043YEARA_

    ^([a-zA-Z0-9]*|(([a-zA-Z0-9])+(([_]*|[-]|[+])[a-zA-Z0-9]+)*))$

它被绞死

这些可能有帮助的案例很少

•积极案例

  1. asdsadas+asd-asdasdad-asdad+asdasd
  2. UPGRADE+asd-asdasdasdvf-asd
  3. UPGRADE+asd-adsasda__LS-TasdL+asdasdwd
  4. UPGRADE+asd-asdasd__LS-TRI+A+L+asdasdwd
  5. UPG-R-ADE+asd-asd__LS-TRI+A+L+asdasdwd
  6. PREREQ+asdasdasda-Traveler-asd3YEAR+FK20000000043YEAR-A
  7. PREREQ+asdasdd-T_raveler-asdasds3YEAR+FK20000000043YEAR-A
  8. UP_G-R+ADE+asd-ESS__________E+N+T-I_A_LS-TRI+A+L+asdasdwd
  9. PREREQ+asdad-Traveler-asdasd3YEAR+FK200__0-0+0_00043YEAR-A
  10. PREREQasdasdasdTravelerasda3YEARFK20000000043YEARA
  11. UP_G-R+ADE+MTG-ESSENTIA__LS-TRI+A+L+asdasdwd
  12. PREREQasdasdasdasdvfTravelerasdt+esasd2K2+0000000+043YEARA
  13. 否定案例

    1. UPG--RADE+asdasd-asdasdasd__LS-TRI+A+L+asdasdwd
    2. UPG-R-ADE_+_asda-asdasdasd__LS-TRI+A+L+asdasdwd
    3. UPG-R-ADE+_asd-asdasdad__LS-TRI+A+L+asdasdwd
    4. UPG-R-ADE+_asd-ESSENTIA__LS-TRI+A+L+asdasdwd
    5. PREREQ+asdasdasdasdvf-asdr-asd++3YEAR+FK20000000043YEAR-A
    6. PREREQ_+-asdasdasdasdvf-asdr-Notes3YEAR+-+FK20000000043YEAR-A
    7. _PREREQ+asdasdasdasdvf-asdr-asd3asdR+FK200__0-0+0_00043YEAR-A
    8. 系统挂起(NEG案例)

      1. PREREQ+asdasdasdasdvf-asd-asd3YEAR+-+FK20000000043asdds-A
      2. PREREQasdasdasdasdvfasdrNot+es3YEARFK2+0000000+043asdasd_

2 个答案:

答案 0 :(得分:0)

您需要使用\w

^\w+([-+]\w+)*$

\w+会多次匹配[a-zA-Z\d_] 1次

答案 1 :(得分:0)

已更正(并经过测试)

看着它:

^
(
    [a-zA-Z0-9]*
    |
    (
        ([a-zA-Z0-9])+
        (
            ([_]*|[-]|[+])
            [a-zA-Z0-9]+
        )*
    )
)
$

可以将其简化为:

^(
    [a-zA-Z0-9]+
    (
        ([_]+|[-+])
        [a-zA-Z0-9]+
    )*
)?$

我还将_*更正为_+,否则下划线将适合每个循环场合的每个位置:指数。导致挂起:(_* ...)*

   ^([a-zA-Z0-9]+(([_]+|[-+])[a-zA-Z0-9]+)*)?$

不幸的是,正则表达式在这些事情上失败了,几乎就像停止问题一样。