正则表达式(有时称为有理表达式)是定义搜索模式的字符序列,主要用于与字符串的模式匹配,或字符串匹配,即类似“查找和替换”的操作。(维基百科)。
正则表达式是一种将模式与字符序列匹配的通用方法。它在每种编程语言中都有使用,如C++、java和Python。
什么是正则表达式?是什么让它如此重要? 正则表达式用于 谷歌分析 在URL匹配中支持搜索和替换在最流行的编辑器中,如Sublime、Notepad++、方括号、Google Docs和Microsoft word。
Example : Regular expression for an email address : ^([a-zA-Z0-9_-.]+)@([a-zA-Z0-9_-.]+).([a-zA-Z]{2,5})$
上述正则表达式可用于检查给定字符集是否为电子邮件地址。
如何编写正则表达式?
- 中继器:*、+和{}: 这些符号充当转发器,告诉计算机前面的字符将被多次使用。
- 星号(*): 它告诉计算机匹配前面的字符(或一组字符)0次或多次(直到无限次)。
Example : The regular expression ab*c will give ac, abc, abbc, abbbc….ans so on
- 加号(+): 它告诉计算机重复前面的字符(或一组字符)至少一次或多次(直到无限次)。
Example : The regular expression ab+c will give abc, abbc, abbc, … and so on.
- 大括号{…}: 它告诉计算机重复前面的字符(或一组字符),重复次数与括号内的值相同。
Example : {2} means that the preceding character is to be repeated 2 times, {min,} means the preceding character is matches min or more times. {min,max} means that the preceding character is repeated at least min & at most max times.
- 通配符–(.) 点符号可以代替任何其他符号,这就是它的原因 被称为通配符。
Example : The Regular expression .* will tell the computer that any character can be used any number of times.
- 可选字符–(?) 这个符号告诉计算机前面的字符可能 或者可能不在要匹配的字符串中。
Example : We may write the format for document file as – “docx?” The ‘?’ tells the computer that x may or may not be present in the name of file format.
- 插入符号(^)符号: 设置比赛位置: 告诉计算机匹配必须从字符串或行的开头开始。
Example : ^d{3} will match with patterns like "901" in "901-333-".
- 美元($)符号 它告诉计算机匹配必须发生在字符串末尾或之前在一行或一行的末尾。
Example : -d{3}$ will match with patterns like "-333" in "-901-333".
- 字符类 字符类匹配一组字符中的任意一个。它用于匹配语言的最基本元素,如字母、数字、空格、符号等。
/s :匹配任何空格字符,如空格和制表符 /S :匹配任何非空白字符 /d :匹配任何数字字符 /D :匹配任何非数字字符 /w :匹配任何单词字符(基本上是字母数字) /W :匹配任何非单词字符 /b :匹配任何单词边界(包括空格、破折号、逗号、分号等)
[设置字符数] –匹配字符集中的任何单个字符。默认情况下,匹配区分大小写。
Example : [abc] will match characters a,b and c in any string.
[^set_个字符]- 否定: 匹配不在字符集_中的任何单个字符。默认情况下,匹配区分大小写。
Example : [^abc] will match any character except a,b,c .
[最后一个] – 字符范围: 匹配从第一个到最后一个范围内的任何单个字符。
Example : [a-zA-z] will match any character from a to z or A to Z.
- 逃生标志:
如果要匹配实际的“+”,“.”etc字符,在该字符之前添加反斜杠()。这将告诉计算机把下面的字符当作搜索字符,并考虑它用于匹配模式。
Example : d+[+-x*]d+ will match patterns like "2+2" and "3*9" in "(2+2) * 3*9".
- 分组字符()
正则表达式的一组不同符号可以组合在一起,作为一个单元,并作为一个块,为此,需要将正则表达式括在括号()中。
Example : ([A-Z]w+) contains two different elements of the regular expression combined together. This expression will match any pattern containing uppercase letter followed by any character.
-
竖条(|): 匹配由竖线(|)字符分隔的任何一个元素。
Example : th(e|is|at) will match words - the, this and that.
- 编号: 反向参考: 允许在同一正则表达式中随后标识先前匹配的子表达式(捕获或括在圆括号内的表达式)。表示第n个括号内的组将在当前位置重复。
Example : ([a-z])1 will match “ee” in Geek because the character at second position is same as character at position 1 of the match.
- 评论:(?#评论)—— 内联注释:注释在第一个右括号处结束。
Example : A(?#This is an inline comment)w+
#[排到最后]: X模式评论。 评论从一个未经修饰的#开始,一直持续到最后。
Example : (?x)Aw+#Matches words starting with A
本文由 阿比纳夫·蒂瓦里 .如果你喜欢GeekSforgek并想投稿,你也可以使用“投稿”撰写文章。极客。组织或邮寄你的文章到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。