简体   繁体   English

正则表达式匹配字符串从开始

[英]Regex Match string from starts with

this is my Regular expression. 这是我的正则表达式。

^AK[0-9A-Za-z-/#\s]{1,15}$

It matches the whole string when we enter some text which starts like'AKtest123' 当我们输入一些像'AKtest123'开头的文字时,它匹配整个字符串

but i want to match the below strings like 但我想匹配下面的字符串

A
AK
AKtest

how can i modify the regex to match the string from the first character? 如何修改正则表达式以匹配第一个字符的字符串?

This should do the magic: 这应该是魔术:

^(A|AK[0-9A-Za-z-/#\s]{0,15})$

That means it will accept A, or AK (notice I changed 1 at the end to 0), or AK and any following 15 characters from the list. 这意味着它将接受A或AK(通知我最后将1更改为0),或AK和列表中的任何后续15个字符。 Is that what you want, right? 这就是你想要的,对吗?

Maybe you're looking for something like this? 也许你正在寻找这样的东西?

^A(?:K(?:[0-9A-Za-z-/#\s]{1,15})?)?$

It will match: 它将匹配:

A
AK
AKtest

I think you need groups. 我认为你需要团体。

Change your regex to: 将你的正则表达式改为:

^(((A)K)[0-9A-Za-z-/#\s]{1,15})$

Group1 will have A , Group2 will have AK and Group3 will have AKtest Group1将拥有A ,Group2将拥有AK而Group3将拥有AKtest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM