perlre

Not all characters can be used 'as is' in a match. Some characters, called metacharacters, are reserved for use in regex notation. The metacharacters are

{}[]()^$.|*+?\

To specifywhere it should match, we would use the anchor metacharacters ^ and $ . The anchor ^ means match at the beginning of the string and the anchor $ means match at the end of the string, or before a newline at the end of the string. Some examples:

 
  1. "housekeeper" =~ /keeper/; # matches
  2. "housekeeper" =~ /^keeper/; # doesn't match
  3. "housekeeper" =~ /keeper$/; # matches
  4. "housekeeper\n" =~ /keeper$/; # matches
  5. "housekeeper" =~ /^housekeeper$/; # matches

Using character classes

character class allows a set of possible characters, rather than just a single character, to match at a particular point in a regex. Character classes are denoted by brackets [...] , with the set of characters to be possibly matched inside. Here are some examples:

 
  1. /cat/; # matches 'cat'
  2. /[bcr]at/; # matches 'bat', 'cat', or 'rat'
  3. "abc" =~ /[cab]/; # matches 'a'

In the last statement, even though 'c' is the first character in the class, the earliest point at which the regex can match is 'a' .

 
  1. /[yY][eE][sS]/; # match 'yes' in a case-insensitive way
  2. # 'yes', 'Yes', 'YES', etc.
  3. /yes/i; # also match 'yes' in a case-insensitive way

The last example shows a match with an 'i' modifier, which makes the match case-insensitive.

Character classes also have ordinary and special characters, but the sets of ordinary and special characters inside a character class are different than those outside a character class. The special characters for a character class are -]\^$ and are matched using an escape:

 
  1. /[\]c]def/; # matches ']def' or 'cdef'
  2. $x = 'bcr';
  3. /[$x]at/; # matches 'bat, 'cat', or 'rat'
  4. /[\$x]at/; # matches '$at' or 'xat'
  5. /[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat'

The special character '-' acts as a range operator within character classes, so that the unwieldy [0123456789] and[abc...xyz] become the svelte [0-9] and [a-z] :

 
  1. /item[0-9]/; # matches 'item0' or ... or 'item9'
  2. /[0-9a-fA-F]/; # matches a hexadecimal digit

If '-' is the first or last character in a character class, it is treated as an ordinary character.

The special character ^ in the first position of a character class denotes a negated character class, which matches any character but those in the brackets. Both [...] and [^...] must match a character, or the match fails. Then

 
  1. /[^a]at/; # doesn't match 'aat' or 'at', but matches
  2. # all other 'bat', 'cat, '0at', '%at', etc.
  3. /[^0-9]/; # matches a non-numeric character
  4. /[a^]at/; # matches 'aat' or '^at'; here '^' is ordinary


  • \d is a digit and represents

       
    1. [0-9]
  • \s is a whitespace character and represents

       
    1. [\ \t\r\n\f]
  • \w is a word character (alphanumeric or _) and represents

       
    1. [0-9a-zA-Z_]
  • \D is a negated \d; it represents any character but a digit

       
    1. [^0-9]
  • \S is a negated \s; it represents any non-whitespace character

       
    1. [^\s]
  • \W is a negated \w; it represents any non-word character

       
    1. [^\w]
  • The period '.' matches any character but "\n"

Matching this or that

We can match different character strings with the alternation metacharacter '|' . To match dog or cat , we form the regex dog|cat . As before, Perl will try to match the regex at the earliest possible point in the string. At each character position, Perl will first try to match the first alternative, dog . If dog doesn't match, Perl will then try the next alternative, cat . If cat doesn't match either, then the match fails and Perl moves to the next position in the string. Some examples:

 
  1. "cats and dogs" =~ /cat|dog|bird/; # matches "cat"
  2. "cats and dogs" =~ /dog|cat|bird/; # matches "cat"

Even though dog is the first alternative in the second regex, cat is able to match earlier in the string.

 
  1. "cats" =~ /c|ca|cat|cats/; # matches "c"
  2. "cats" =~ /cats|cat|ca|c/; # matches "cats"

At a given character position, the first alternative that allows the regex match to succeed will be the one that matches. Here, all the alternatives match at the first string position, so the first matches.

Grouping things and hierarchical matching

The grouping metacharacters () allow a part of a regex to be treated as a single unit. Parts of a regex are grouped by enclosing them in parentheses. The regex house(cat|keeper) means match house followed by either cat orkeeper . Some more examples are

 
  1. /(a|b)b/; # matches 'ab' or 'bb'
  2. /(^a|b)c/; # matches 'ac' at start of string or 'bc' anywhere
  3. /house(cat|)/; # matches either 'housecat' or 'house'
  4. /house(cat(s|)|)/; # matches either 'housecats' or 'housecat' or
  5. # 'house'. Note groups can be nested.
  6. "20" =~ /(19|20|)\d\d/; # matches the null alternative '()\d\d',
  7. # because '20\d\d' can't match


PS:[] 与 () 的区别,前者只能命中单个字符,后者无此限制


Matching repetitions

The quantifier metacharacters ?* , + , and {} allow us to determine the number of repeats of a portion of a regex we consider to be a match. Quantifiers are put immediately after the character, character class, or grouping that we want to specify. They have the following meanings:

  • a? = match 'a' 1 or 0 times

  • a* = match 'a' 0 or more times, i.e., any number of times

  • a+ = match 'a' 1 or more times, i.e., at least once

  • a{n,m} = match at least n times, but not more than m times.

  • a{n,} = match at least n or more times

  • a{n} = match exactly n times

Here are some examples:

 
  1. /[a-z]+\s+\d*/; # match a lowercase word, at least some space, and
  2. # any number of digits
  3. /(\w+)\s+\g1/; # match doubled words of arbitrary length
  4. $year =~ /^\d{2,4}$/; # make sure year is at least 2 but not more
  5. # than 4 digits
  6. $year =~ /^\d{4}$|^\d{2}$/; # better match; throw out 3 digit dates

Windows 系统修复工具主要用于解决 Windows 11/10 系统中的各种常见问题,具有操作简单、功能全面等特点: 文件资源管理器修复:可解决文件资源管理器卡死、崩溃、无响应等问题,能终止崩溃循环。还可修复右键菜单无响应或选项缺失问题,以及重建缩略图缓存,让图片、视频等文件的缩略图正常显示,此外,还能处理桌面缺少回收站图标、回收站损坏等问题。 互联网和连接修复:能够刷新 DNS 缓存,加速网页加载速度,减少访问延迟。可重置 TCP/IP 协议栈,增强网络连接稳定性,减少网络掉线情况,还能还原 Hosts 文件,清除恶意程序对网络设置的篡改,保障网络安全,解决电脑重装系统后网络无法连接、浏览器主页被篡改等问题。 系统修复:集成系统文件检查器(SFC),可自动扫描并修复受损的系统文件。能解决 Windows 激活状态异常的问题,还可重建 DLL 注册库,恢复应用程序兼容性,解决部分软件无法正常运行的问题,同时也能处理如 Windows 沙箱无法启动、Windows 将 JPG 或 JPEG 保存为 JFIF 等系统问题。 系统工具维护:提供启动管理器、服务管理器和进程管理器等工具,用户可控制和管理启动程序、系统服务和当前运行的进程,提高系统的启动和运行速度,防止不必要的程序和服务占用系统资源。还能查看系统规格,如处理器线程数、最大显示分辨率等。 故障排除:集成超过 20 个微软官方诊断工具,可对系统问题进行专业排查,还能生成硬件健康状态报告。能解决搜索和索引故障、邮件和日历应用程序崩溃、设置应用程序无法启动等问题,也可处理打印机、网络适配器、Windows 更新等相关故障。 其他修复功能:可以重置组策略设置、catroot2 文件夹、记事本等多种系统设置和组件,如重置 Windows 应用商店缓存、Windows 防火墙设置等。还能添加重建图标缓存支持,恢复粘滞便笺删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值