gpt4 book ai didi

c# - 如何为以下条件创建正则表达式?

转载 作者:行者123 更新时间:2023-12-02 00:33:37 25 4
gpt4 key购买 nike

我正在为以下条件创建正则表达式

字符串可以包含任何字母、数字以及 ' 和 ?字符串应以字母或数字开头

例如:

adsfj
asfj's
jfkd'sdf?
df
ds?
afjdk?

全部有效

我使用C# 2.0

我尝试过这样的事情

  ^[a-zA-Z0-9]+[']\*[a-zA-Z0-9]\*[?]\*[a-zA-Z0-9]\*$

这并没有解决问题......有什么想法......?

最佳答案

它比你做的要简单:^[a-zA-Z\d][a-zA-Z\d'?]*$

^            # Start of string anchor.
[a-zA-Z\d] # First character is a letter or a digit.
[a-zA-Z\d'?] # Subsequent characters are letters, digits, apostrophes or question marks...
* # ...repeated any number of times.
$ # Until the end of the string.

如果您允许使用下划线与其他字符,则可以将其简化为:^\w[\w'?]*$

关于c# - 如何为以下条件创建正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2219995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com