看实例学正则表达式
首先,让我们看看两个非凡的字符:’^’ 和 ‘$’ 他们是分别用来匹配字符串的开始和结束,一下分别举例说明:
"^The": 匹配以 "The"开头的字符串;
"of despair$": 匹配以 "of despair" 结尾的字符串;
"^abc$": 匹配以abc开头和以abc结尾的字符串,实际上是只有abc与之匹配;
"notice": 匹配包含notice的字符串;
你可以看见假如你没有用我们提到的两个字符(最后一个例子),就是说 模式(正则表达式) 可以出现在被检验字符串的任何地方,你没有把他锁定到两边。
这里还有几个字符 '*', ' ',和 '?', 他们用来表示一个字符可以出现的次数或者顺序. 他们分别表示:"zero or more", "one or more", and "zero or one." 这里是一些例子:
"ab*": 匹配字符串a和0个或者更多b组成的字符串("a", "ab", "abbb", etc.);
"ab ": 和上面一样,但最少有一个b ("ab", "abbb", etc.);
"ab?":匹配0个或者一个b;
"a?b $": 匹配以一个或者0个a再加上一个以上的b结尾的字符串.
你也可以在大括号里面限制字符出现的个数,比如
"ab{2}": 匹配一个a后面跟两个b(一个也不能少)("abb");
"ab{2,}": 最少更两个b("abb", "abbbb", etc.);
"ab{3,5}": 2-5个b("abbb", "abbbb", or "abbbbb").
你还要注重到你必须总是指定 (i.e, "{0,2}", not "{,2}").同样,你必须注重到, '*', ' ', 和'?' 分别和一下三个范围标注是一样的,"{0,}", "{1,}", 和 "{0,1}"。
现在把一定数量的字符放到小括号里,比如:
"a(bc)*": 匹配 a 后面跟0个或者一个"bc";
"a(bc){1,5}": 一个到5个 "bc."
还有一个字符 '│', 相当于OR 操作:
"hi│hello": 匹配含有"hi" 或者 "hello" 的 字符串;
"(b│cd)ef": 匹配含有 "bef" 或者 "cdef"的字符串;
"(a│B)*c": 匹配含有这样 - 多个(包括0个)a或b,后面跟一个c的字符串 的字符串;
一个点('.')可以代表所有的 单一字符:
"a.[0-9]": 一个a跟一个字符再跟一个数字的 (含有这样一个字符串的字符串将被匹配,以后省略此括号)
"^.{3}$": 以三个字符结尾.中括号括住的内容只匹配一个 单一的字符
"[ab]": 匹配单个的 a 或者 b ( 和 "a│b" 一样);
"[a-d]": 匹配'a' 到'd'的单个字符 (和"a│b│c│d" 还有 "[abcd]"效果一样);
"^[a-zA-Z]": 匹配以字母开头的字符串
"[0-9]%": 匹配含有 形如 x% 的字符串
",[a-zA-Z0-9]$": 匹配以逗号在加一个数字或字母结尾的字符串
你也可以把你不想要得字符列在中括号里,你只需要在总括号里面使用'^' 作为开头 (i.e., "%[^a-zA-Z]%" 匹配含有 两个百分号里面有一个非字母 的字符串).
为了能够解释,但"^.[$()│* ?{"作为有非凡意义的字符的时候,你必须在这些字符面前加'', 还有在php3中你应该避免在模式的最前面使用, 比如说,正则表达式 "($│?[0-9] " 应该这样调用 ereg("($│?[0-9] ", $str) (不知道php4是不是一样)
不要忘记在中括号里面的字符是这条规路的例外

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

Each season of Teamfight Tactics lasts about three months. Currently, the US test server of Teamfight Tactics S11 season will be updated and launched on March 7. Teamfight Tactics and Golden Shovel will be updated and launched on March 21. It is speculated that the S11 season It will probably end in early July. When will TFT S11 end? Answer: Early July. 1. It is speculated that the S11 season will end in early July. The specific end date needs to wait for the official announcement. 2. Each season of Teamfight Tactics lasts about three months. 3. The US test server of Teamfight Tactics S11 season will be updated and launched on March 7, and Teamfight Tactics and Golden Shovel will be updated and launched on March 21. 4. A new gameplay mechanism will be added to the S11 season, and more than 20 new Ornn artifacts will be added.

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

Strings in GoLang, although immutable, can be dynamically modified using the following technique: concatenating strings using string concatenation. Create a new string using string formatting. Modify the underlying byte slice of the string. Use mutable string types provided by third-party libraries.
