正则表达式为非连续大写小写 [英] Regex for non-consecutive uppercase with lowercase

查看:303
本文介绍了正则表达式为非连续大写小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: string s(AAA);

std :: smatch m;

std :: regex e((?=。{3, } [AZ])(?=。{0,} [az])。*);

output = std :: regex_search(s,m,e);



这里它应该有3个或更多的大写字母和零个或多个小写字母。

然而输出为零意味着它失败。当我尝试用1替换零和

s(AAA4)时它工作正常。



所以现在我希望它允许零或更多,但似乎它不接受零

我甚至尝试量词(*)相当于{0,}仍然不起作用。



我尝试过:



我也试过[AZ] {3,} [az ] * 
但这将允许:
s(AAA)和s(AAAb)但现在是s(AbAA)
,因为只有当鞋帮连续时才允许。 / pre>

解决方案

您需要添加字符串代码的开头和结尾。

试试这个:

 ^ [AZ] {3,} [az] * 




你的正则表达式需要两个未被捕获的后缀,这很奇怪。



获取 Expresso [ ^ ] - 免费,它检查并生成正则表达式。


std::string s("AAA");
std::smatch m;
std::regex e("(?=.{3,}[A-Z])(?=.{0,}[a-z]).*");
output = std::regex_search(s, m, e);

Here it should have 3 or more uppercase letters and zero or more lowercase letter.
However the output is zero meaning it fails. When I try replace zero with 1 and
s("AAA4") it works fine.

So now I want it to allow zero or more but it seems like it is not accepting zero
I even tried quantifier (*) which is equivalent to {0,} still not working.

What I have tried:

.

I also tried [A-Z]{3,}[a-z]* 
but this will allow:
s("AAA") and s("AAAb") but now s("AbAA")
as it will allow only if upper are consecutive.

解决方案

You need to add the start and end of string codes.
Try this:

^[A-Z]{3,}[a-z]*



Your Regex requires two suffixes which aren't captured, which is rather odd.

Get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.


这篇关于正则表达式为非连续大写小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆