在这里,我们将给大家分享关于Python-正则表达式,用于检测&循环的分号终止的C++的知识,让您更了解python正则判断的本质,同时也会涉及到如何更有效地Java正则表达式,用于检测类/接口/等声
在这里,我们将给大家分享关于Python-正则表达式,用于检测&循环的分号终止的C ++的知识,让您更了解python 正则判断的本质,同时也会涉及到如何更有效地Java正则表达式,用于检测类/接口/等声明、js中正则表达式与Python中正则表达式的区别、php – 用于检测提及但未检测到电子邮件的正则表达式、PHP正则表达式,用于强大的密码验证的内容。
本文目录一览:- Python-正则表达式,用于检测&循环的分号终止的C ++(python 正则判断)
- Java正则表达式,用于检测类/接口/等声明
- js中正则表达式与Python中正则表达式的区别
- php – 用于检测提及但未检测到电子邮件的正则表达式
- PHP正则表达式,用于强大的密码验证
Python-正则表达式,用于检测&循环的分号终止的C ++(python 正则判断)
在我的Python应用程序中,我需要编写一个与以分号()终止的C ++ for
或while
循环匹配的正则表达式;。例如,它应与此匹配:
for (int i = 0; i < 10; i++);
…但是不是这个:
for (int i = 0; i < 10; i++)
乍一看,这似乎很琐碎,直到你意识到左括号和右括号之间的文本可能包含其他括号,例如:
for (int i = funcA(); i < funcB(); i++);
我正在使用python.re模块。现在,我的正则表达式如下所示(我留了我的评论,以便你可以更轻松地理解它):
# match any line that begins with a "for" or "while" statement:^\s*(for|while)\s*\( # match the initial opening parenthesis # Now make a named group ''balanced'' which matches a balanced substring. (?P<balanced> # A balanced substring is either something that is not a parenthesis: [^()] | # …or a parenthesised string: \( # A parenthesised string begins with an opening parenthesis (?P=balanced)* # …followed by a sequence of balanced substrings \) # …and ends with a closing parenthesis )* # Look for a sequence of balanced substrings\) # Finally, the outer closing parenthesis.# must end with a semi-colon to match:\s*;\s*
这对于上述所有情况都非常适用,但是一旦你尝试使for循环的第三部分包含一个函数,它就会中断,如下所示:
for (int i = 0; i < 10; doSomethingTo(i));
我认为这是有问题的,因为只要你在开始和结束括号之间放置一些文本,“平衡”组就会匹配包含该文本的文本,因此该(?P=balanced)
部分不再起作用,因为它不匹配(由于事实括号内的文本是不同的)。
在我的Python代码中,我使用VERBOSE和MULTILINE标志,并创建正则表达式,如下所示:
REGEX_STR = r"""# match any line that begins with a "for" or "while" statement:^\s*(for|while)\s*\( # match the initial opening parenthesis # Now make a named group ''balanced'' which matches # a balanced substring. (?P<balanced> # A balanced substring is either something that is not a parenthesis: [^()] | # …or a parenthesised string: \( # A parenthesised string begins with an opening parenthesis (?P=balanced)* # …followed by a sequence of balanced substrings \) # …and ends with a closing parenthesis )* # Look for a sequence of balanced substrings\) # Finally, the outer closing parenthesis.# must end with a semi-colon to match:\s*;\s*"""REGEX_OBJ = re.compile(REGEX_STR, re.MULTILINE| re.VERBOSE)
有人可以建议对此正则表达式进行改进吗?对于我来说,变得太复杂了以至于无法理解。
答案1
小编典典你可以编写一个非常简单的例程来执行此操作,而无需使用正则表达式:
- 设置一个位置计数器
pos
,使其指向你for
或之后的左方括号while
。 - 将方括号计数器设置
openBr
为0。 - 现在继续递增
pos
,读取各个位置的字符,并openBr
在看到方括号时增加,而在看到方括号时减少。这将在开始一次增加它,在“第一左括号for
(”,递增和递减多一些对之间的一些支架,并将其设置回0
当你for
托架关闭。 - 所以,不要当
openBr
是0一次。
停止位置是的右括号for(...)
。现在,你可以检查后面是否有分号。
Java正则表达式,用于检测类/接口/等声明
我正在尝试创建一个检测新类的正则表达式,例如:
public interface IGame {
要么
private class Game {
这是我到目前为止所没有的,但是没有检测到:
(line.matches("(public|protected|private|static|\\s)"+"(class|interface|\\s)"+"(\\w+)"))
任何人都可以给我一些指示吗?
答案1
小编典典如下更改您的正则表达式以匹配两种类型的字符串格式。
line.matches("(?:public|protected|private|static)\\s+(?:class|interface)\\s+\\w+\\s*\\{");
例:
String s1 = "public interface IGame {";String s2 = "private class Game {";System.out.println(s1.matches("(?:public|protected|private|static)\\s+(?:class|interface)\\s+\\w+\\s*\\{"));System.out.println(s2.matches("(?:public|protected|private|static)\\s+(?:class|interface)\\s+\\w+\\s*\\{"));
输出:
truetrue
js中正则表达式与Python中正则表达式的区别
今天女票让我帮她写一个js中的正则,来提取电话号码,对于正则规则来说,js与python是基本没有区别的,重点的区别是在一些函数与方法中。
python中的正则提取:
import re
str = ''asfasdfgasffas青蛙无法·啊沙发上,。,从 dw2efdrqw15894648760asfasf'' \
''asf,./asf029-81464970jhklasdnf,wsdn15888888888''
patt = ''1[3,5,8,7]\d{9}|0\d{2}-\d{8}''
data = re.findall(patt, str)
print(data)
js中的正则提取:
<script>
$(''#id1'').click(function () {
var test = ''asfasdfhakj啊是法律dsh15894648760asfadf欺负我,。, 。qwe jfwj029-81464970asfafaflame'';
var patt= /1[3,5,8,7]\d{9}|0\d{2}-\d{8}/g;
alert(test.match(patt))
})
</script>
两者主要区别:
(1)在python中,正则表达式其本质上是一个字符串,所以用引号括起来,但是在JS中,正则必须以斜杠符号 / 来包围,如
/1[3,5,8,7]\d{9}|0\d{2}-\d{8}/ 并且不能加引号。
(2)在匹配模式中,js仅仅支持 g 全局搜索, i忽略大小写, m 多行匹配这三种, 但是python支持更多。
(3)js中匹配并替换是replace函数,在python中是sub() js中的replace无论使用正则表达式还是字符串本身,默认情况下仅仅替换第一个匹配项。
(4)JS以 / 来标识正则表达式,以引号标识字符串。
(5) JS与Python都有match函数,但是意义大不相同。Python中match方法是指从字符串开始进行匹配,但是在js中match方法是类似于匹配全部,
与python中的findall()非常接近。但是还是有区别滴: 如果在js的正则中后一个/后加上g,那就与python中的findall一毛一样。会返回所有匹配到的值,
但是如果没有g,就只会返回第一个匹配到的值
(6) JS 中的search()与Python中字符串的find方法和正则表达式re模块中的search方法一样, 但是js会返回字符串的下标索引,而非字符串。
还有疏忽的地方,来日想起来再补充吧。这儿有个别的大佬写的,栗子啥的挺全的:
大佬链接:https://www.cnblogs.com/dyfblog/p/6077122.html
想了解更多Python关于爬虫、数据分析的内容,欢迎大家关注我的微信公众号:悟道Python
php – 用于检测提及但未检测到电子邮件的正则表达式
preg_match('/@([^@ ]+)/',$image->caption->text,$matches)
我想基本上检测字符串中的提及.然而现在的问题是它与电子邮件地址混淆,因此它检测到电子邮件作为提及,因此例如如果我有aksdjasd@yahoo.com,那么这算作匹配.我想我想说的是在@符号之前应该有一个空格.但是我怎么把它放到这个正则表达式?
编辑:
我也想在字符串的开头检测@mentions
解决方法
before the @ sign there should be a space
您可以使用lookbehind(根据OP的评论编辑):
preg_match('/(?<= |^)@[^@ ]+/',$matches);
Working Demo
PHP正则表达式,用于强大的密码验证
我已经在网上看到以下正则表达式。
(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$
仅当字符串:
* contain at least (1) upper case letter
* contain at least (1) lower case letter
* contain at least (1) number or special character
* contain at least (8) characters in length
我想知道如何转换此正则表达式,以便将字符串检查为
* contain at least (2) upper case letter
* contain at least (2) lower case letter
* contain at least (2) digits
* contain at least (2) special character
* contain at least (8) characters in length
好吧,如果它至少包含2个大写,小写,数字和特殊字符,那么我就不需要8个字符的长度。
特殊字符包括:
`〜!@#$%^&*()_- + = [] \ | {} ;:’“。,/ <>?
今天关于Python-正则表达式,用于检测&循环的分号终止的C ++和python 正则判断的讲解已经结束,谢谢您的阅读,如果想了解更多关于Java正则表达式,用于检测类/接口/等声明、js中正则表达式与Python中正则表达式的区别、php – 用于检测提及但未检测到电子邮件的正则表达式、PHP正则表达式,用于强大的密码验证的相关知识,请在本站搜索。
本文标签: