douqiang5933 2010-09-29 04:39
浏览 340
已采纳

正则表达式有助于匹配多个字母

I am using the following regex to match an account number. When we originally put this regex together, the rule was that an account number would only ever begin with a single letter. That has since changed and I have an account number that has 3 letters at the beginning of the string.

I'd like to have a regex that will match a minimum of 1 letter and a maximum of 3 letters at the beginning of the string. The last issue is the length of the string. It can be as long as 9 characters and a minimum of 3.

Here is what I am currently using.

'/^([A-Za-z]{1})([0-9]{7})$/'

Is there a way to match all of this?

  • 写回答

2条回答 默认 最新

  • dsymx68408 2010-09-29 04:42
    关注

    You want:

    ^[A-Za-z]([A-Za-z]{2}|[A-Za-z][0-9]|[0-9]{2})[0-9]{0,6}$
    

    The initial [A-Za-z] ensures that it starts with a letter, the second bit ([A-Za-z]{2}|[A-Za-z][0-9]|[0-9]{2}) ensures that it's at least three characters long and consists of between one and three letters at the start, and the final bit [0-9]{0,6} allows you to go up to 9 characters in total.

    Further explaining:

    ^                    Start of string/line anchor.
    [A-Za-z]             First character must be alpha.
    ( [A-Za-z]{2}        Second/third character are either alpha/alpha,
     |[A-Za-z][0-9]       alpha/digit,
     |[0-9]{2}            or digit/digit
    )                      (also guarantees minimum length of three).
    [0-9]{0,6}           Then up to six digits (to give length of 3 thru 9).
    $                    End of string/line marker.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?