|
359 | 359 | 定义一个前置约束(存在)要使用 `()`. 在括号内部使用一个问号和等号: `(?=...)`. |
360 | 360 |
|
361 | 361 | 前置约束的内容写在括号中的等号后面. |
362 | | -例如, 表达式 `[T|t]he(?=\sfat)` 匹配 `The` 和 `the`, 在括号中我们又定义了前置约束(存在) `(?=\sfat)` ,即 `The` 和 `the` 后面紧跟着 `(空格)fat`. |
| 362 | +例如, 表达式 `(T|t)he(?=\sfat)` 匹配 `The` 和 `the`, 在括号中我们又定义了前置约束(存在) `(?=\sfat)` ,即 `The` 和 `the` 后面紧跟着 `(空格)fat`. |
363 | 363 |
|
364 | 364 | <pre> |
365 | | -"[T|t]he(?=\sfat)" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on the mat. |
| 365 | +"(T|t)he(?=\sfat)" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on the mat. |
366 | 366 | </pre> |
367 | 367 |
|
368 | 368 | [在线练习](https://regex101.com/r/IDDARt/1) |
|
372 | 372 | 前置约束-排除 `?!` 用于筛选所有匹配结果, 筛选条件为 其后不跟随着定义的格式 |
373 | 373 | `前置约束-排除` 定义和 `前置约束(存在)` 一样, 区别就是 `=` 替换成 `!` 也就是 `(?!...)`. |
374 | 374 |
|
375 | | -表达式 `[T|t]he(?!\sfat)` 匹配 `The` 和 `the`, 且其后不跟着 `(空格)fat`. |
| 375 | +表达式 `(T|t)he(?!\sfat)` 匹配 `The` 和 `the`, 且其后不跟着 `(空格)fat`. |
376 | 376 |
|
377 | 377 | <pre> |
378 | | -"[T|t]he(?!\sfat)" => The fat cat sat on <a href="#learn-regex"><strong>the</strong></a> mat. |
| 378 | +"(T|t)he(?!\sfat)" => The fat cat sat on <a href="#learn-regex"><strong>the</strong></a> mat. |
379 | 379 | </pre> |
380 | 380 |
|
381 | 381 | [在线练习](https://regex101.com/r/V32Npg/1) |
382 | 382 |
|
383 | 383 | ### 4.3 `?<= ...` 后置约束-存在 |
384 | 384 |
|
385 | 385 | 后置约束-存在 记作`(?<=...)` 用于筛选所有匹配结果, 筛选条件为 其前跟随着定义的格式. |
386 | | -例如, 表达式 `(?<=[T|t]he\s)(fat|mat)` 匹配 `fat` 和 `mat`, 且其前跟着 `The` 或 `the`. |
| 386 | +例如, 表达式 `(?<=(T|t)he\s)(fat|mat)` 匹配 `fat` 和 `mat`, 且其前跟着 `The` 或 `the`. |
387 | 387 |
|
388 | 388 | <pre> |
389 | | -"(?<=[T|t]he\s)(fat|mat)" => The <a href="#learn-regex"><strong>fat</strong></a> cat sat on the <a href="#learn-regex"><strong>mat</strong></a>. |
| 389 | +"(?<=(T|t)he\s)(fat|mat)" => The <a href="#learn-regex"><strong>fat</strong></a> cat sat on the <a href="#learn-regex"><strong>mat</strong></a>. |
390 | 390 | </pre> |
391 | 391 |
|
392 | 392 | [在线练习](https://regex101.com/r/avH165/1) |
|
397 | 397 | 例如, 表达式 `(?<!(T|t)he\s)(cat)` 匹配 `cat`, 且其前不跟着 `The` 或 `the`. |
398 | 398 |
|
399 | 399 | <pre> |
400 | | -"(?<![T|t]he\s)(cat)" => The cat sat on <a href="#learn-regex"><strong>cat</strong></a>. |
| 400 | +"(?<!(T|t)he\s)(cat)" => The cat sat on <a href="#learn-regex"><strong>cat</strong></a>. |
401 | 401 | </pre> |
402 | 402 |
|
403 | 403 | [在线练习](https://regex101.com/r/8Efx5G/1) |
|
0 commit comments