Using $ anchor inside a character class does not work

前端 未结 2 1348
逝去的感伤
逝去的感伤 2021-01-25 17:40

I am trying to write a Regex to match a String having 1st 10 characters capital-alpha numeric and then a space and some other text or a String having 1st 10 alpha numeric charac

相关标签:
2条回答
  • 2021-01-25 18:11

    The $ symbol in [...] is treated literally. Use alternation to require either "space followed by something" or "end of line":

    [A-Z0-9]{10}(?:\s.*|$)
    

    Demo: https://regex101.com/r/f4ufzx/1

    0 讨论(0)
  • 2021-01-25 18:11

    You don't need angular brackets for space, try below:

    System.out.println("MNA345QWRE ABC".matches("[A-Z0-9]{10}(\\s|$).*"));
    System.out.println("MNA345QWRE_ABC".matches("[A-Z0-9]{10}(\\s|$)*"));
    System.out.println("MNA345QWRE".matches("[A-Z0-9]{10}(\\s|$).*"));
    
    0 讨论(0)
提交回复
热议问题