2020-04-17重新编辑学习
校验用户输入格式,例如是否为邮箱
mysql中使用正则表达式
mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';
问题:mysql中使用正则表达式会导致查询效率下降吗?原因是什么
ElasticSearch中使用正则表达式
GET /_search
{
"query": {
"regexp": {
"user": {
"value": "k.*y",
"flags" : "ALL",
"max_determinized_states": 10000,
"rewrite": "constant_score"
}
}
}
}
MongoDB 正则表达式
db.posts.find({post_text:{$regex:"hello[A-Z]"}})
Regular Expression 中文正则表达式,目前最常见的应用是作为一种匹配模式。
一个正则表达式通常被称为一个模式(pattern),为用来描述或者匹配一系列符合某个句法规则的字符串
对于我来说,我曾使用较多的场景是通过grep这个命令。
提起正则表达式,其实这里面还要说到一个人Kenneth Lane Thompson,
这尊神几乎以一己之力奠定了人类计算机操作系统的基础。
关于grep的历史可以参考这篇文章
greedy 贪婪,可以理解为尽可能多的
Non-greedy 非贪婪,可以理解为尽可能少的
re.py读一遍,比任何书籍的收获都要大。
Support for regular expressions (RE).
This module provides regular expression matching operations similar to
those found in Perl. It supports both 8-bit and Unicode strings; both
the pattern and the strings being processed can contain null bytes and
characters outside the US ASCII range.
Regular expressions can contain both special and ordinary characters.
Most ordinary characters, like "A", "a", or "0", are the simplest
regular expressions; they simply match themselves. You can
concatenate ordinary characters, so last matches the string 'last'.
The special characters are:
"." Matches any character except a newline.
"^" Matches the start of the string.
"$" Matches the end of the string or just before the newline at
the end of the string.
"*" Matches 0 or more (greedy) repetitions of the preceding RE.
Greedy means that it will match as many repetitions as possible.
"+" Matches 1 or more (greedy) repetitions of the preceding RE.
"?" Matches 0 or 1 (greedy) of the preceding RE.
*?,+?,?? Non-greedy versions of the previous three special characters.
{m,n} Matches from m to n repetitions of the preceding RE.
{m,n}? Non-greedy version of the above.
"\\" Either escapes special characters or signals a special sequence.
[] Indicates a set of characters.
A "^" as the first character indicates a complementing set.
"|" A|B, creates an RE that will match either A or B.
(...) Matches the RE inside the parentheses.
The contents can be retrieved or matched later in the string.
(?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
(?:...) Non-grouping version of regular parentheses.
(?P...) The substring matched by the group is accessible by name.
(?P=name) Matches the text matched earlier by the group named name.
(?#...) A comment; ignored.
(?=...) Matches if ... matches next, but doesn't consume the string.
(?!...) Matches if ... doesn't match next.
(?<=...) Matches if preceded by ... (must be fixed length).
(?
import
re.compile()
re.search()
re.match()
re.findall()
当pattern用括号进行分组时,第一个括号是group(1),以此类推
.group()
.group(1)
.groups()