V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
gaozhy
0.01D
V2EX  ›  问与答

正则表达式 只能输入数字、小数和字母 应该怎么写

  •  
  •   gaozhy · Jun 10, 2022 · 1675 views
    This topic created in 1425 days ago, the information mentioned may be changed or developed.
    满足规则:
    - 11.1
    - 11
    - "Abc"
    - "Abc11.0"
    - "Abc11.1d"

    不满足规则:
    - "Abc11.d" 11.d 不是数字
    9 replies    2022-06-10 21:37:11 +08:00
    ink19
        1
    ink19  
       Jun 10, 2022
    11.也可以认为是数字吧
    gaozhy
        2
    gaozhy  
    OP
       Jun 10, 2022
    @ink19 11. 不满足规则 需要是数字或者小数出现
    longbow0
        3
    longbow0  
       Jun 10, 2022
    abc1.1.1abc 算不算?
    just1
        4
    just1  
       Jun 10, 2022
    ^(\d+(\.\d+)?|[a-zA-Z]+)+$
    superychen
        5
    superychen  
       Jun 10, 2022
    示例里带字母的情况下都加了双引号,双引号算不算在里面?
    darklights
        6
    darklights  
       Jun 10, 2022
    @just1 思路跟你差不多,但是无法排除 1.11.1x
    superychen
        7
    superychen  
       Jun 10, 2022
    试试这个

    ^\S[a-zA-Z]*(\d+(.\d+)?d?)?$
    just1
        8
    just1  
       Jun 10, 2022   ❤️ 1
    @darklights ^((\d+(\.\d+)?(?!\d))|[a-zA-Z])+$
    数字末尾加个否定就行了
    gaozhy
        9
    gaozhy  
    OP
       Jun 10, 2022
    @just1
    @superychen 谢谢各位老哥~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   787 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 83ms · UTC 20:19 · PVG 04:19 · LAX 13:19 · JFK 16:19
    ♥ Do have faith in what you're doing.