dou91736 2017-03-13 04:15
浏览 39
已采纳

REGEX - 字符串不能从这些单词开始,并且可能包含字母数字字符

Requirement are as follows:

1.First three characters must not be "CP ","C P". Note there is a space in both these words
2.First character may be alphanumeric, including À, Â, Ç, È
3.2nd and 3rd caracter can be any ascii character other than (0-31)

The part that I am having trouble with is how can I write regex where the string does not start with "CP " word or "C P" that word. Note that ASCII 32 is space character which is valid in regex match but not but in combination with "CP ".

I have tried something like this as starting point but to no avail

(?!CP )[^\x00-\x1F]

Sample Strings

invalid text : CP 420, C P 420

Valid text: abc 420, 420 CP etc.

  • 写回答

2条回答 默认 最新

  • dthyxna3894 2017-03-13 04:42
    关注

    You need a starting anchor, you need to add the optional whitespace into the negative lookahead. It will be easier to list the characters you want than to list the characters you don't, since it is 2 different ranges. Try:

    ^(?!(?:C ?P|CP ?))[A-Za-z\dÀÂÇÈ][\x20-\xFF]{1,2}
    

    Demo: https://regex101.com/r/4Rhv2V/2/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起