duanlisha2335 2016-01-30 01:07
浏览 50
已采纳

使用RegEx计算匹配模式中字符串的长度

I've been trying to resolve this all day.

Here are the requirements:
- I want to match strings that optionally starts with " _ "
- Should then continue with one or more case-insensitive alphabet
- Should then contain an optional " _ " or " - " followed by case-insensitive alphanumeric characters (This step should match at lease once).
- Then it may optionally end with " _ "

The above requirements are not a problem 'cos I achieved that with:

/_?[A-Za-z]([-_]?[A-Za-z0-9])+_?/

The above RegEx matches:

  • user_
  • usEr-nAme
  • uSer_naMe-Two_
  • _Userna-Me_tHree-and-four

THE PROBLEM
How do I make it match only 6 to 25 characters?

I Tried

/(_?[A-Za-z]([-_]?[A-Za-z0-9])+_?){6,25}/

but it still matches string less than 6 like Use-r

  • 写回答

1条回答 默认 最新

  • drqja5919276 2016-01-30 01:42
    关注

    Put a lookahead assertion at the start:

    (?=^.{6,25}$)_?[A-Za-z]([-_]?[A-Za-z0-9])+_?
    

    Demo here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题