The introduction of Regex

定义
在一个文件或字符里查找和替代文本的一种标准,就是用一个“字符串”来描述一个特征,然后去验证另一个“字符串”是否符合这个特征。
用途:
验证字符串是否符合指定特征,比如验证是否是合法的邮件地址。
用来查找字符串,从一个长的文本中查找符合指定特征的字符串,比查找固定字符串更加灵活方便。
用来替换,比普通的替换更强大。

PCREPerl Compatible Regular Expression. PERL兼容正则可以使用任何不是字母、数字或反斜线(\)的字符作为定界符,如果作为定界符的字符必须被用在表达式本身中,则需要用反斜线转义。也可以使用(){}[]<>作为定界符修正符: PERL兼容正则中可能使用的修正符(修正符中的空格和换行被忽略,其它字符会导致错误)
POSIX的全称是Portable Operating System Interface foruniX,它由一系列规范构成,定义了UNIX操作系统应当支持的功能。
PCREPOSIX正则表达式的不同
1)POSIX UNIX 遵循的标准, UNIX的命令如grepsed能用的正则是POSIXPERL正则在POSIX上做了扩展,实现了很多方便的功能。
2)PCRE函数需要模式以分隔符闭合. POSIX兼容正则没有定界符,函数的相应参数会被认为是正则。POSIX兼容正则没有修正符。
3)匹配数字的例子,Perl\dPOSIX[0-9](POSIX新版本已支持\d)、\w\s
4)不像POSIX,PCRE扩展没有专门用于大小写不敏感匹配的函数.取而代之的是,支持使用/i模式修饰符完成同样的工作.其他模式修饰符同样可用于改变匹配策略.
5)POSIX函数从最左面开始寻找最长的匹配,但是PCRE在第一个合法匹配后停止.如果字符串 不匹配这没有什么区别,但是如果匹配,两者在结果和速度上都会有差别.

BRE: Linux/Unix常用工具中,grepvised都属于BRE这一派,它的语法看起来比较奇怪,元字符『(』『)』『{』『}』必须转义之后才具有特殊含义,所以正则表达式『(a)b』只能匹配字符串(a)b而不是字符串ab;正则表达式『a{1,2}』只能匹配字符串a{1,2},正则表达式『a\{1,2\}』才能匹配字符串a或者aaBRE不支持『+』『?』量词,也不支持多选结构『(…|…)』和反向引用『\1』『\2』…
GNUBREGNUBRE做了扩展,支持『+』『?』『|』,只是使用时必须写成『\+』『\?』『\|』,而且也支持『\1』『\2』之类反向引用
ERE:并不要求兼容BRE的语法,而是自成一体。因此其中的元字符不用转义(在元字符之前添加反斜线会取消其特殊含义),所以『(ab|cd)』就可以匹 配字符串ab或者cd,量词『+』『?』『{n,m}』可以直接使用。ERE并没有明确规定支持反向引用,但是不少工具都支持『\1』『\2』之类 的反向引用。




Application

grep全词匹配:
1)grep -w 'aicentaicent_cm.cfg (只匹配数字、字母,不能过滤符号)
2)grep '\<aicent\>' aicent_cm.cfg
3)grep '\baicent\b' aicent_cm.cfgsolaris\b无效)
grep严格匹配:
1)aicent
2)#aicent
3)aicent@

  grep '^aicent$' aicent_cm.cfg

1)aicent  aicent

  一行的开头是aicent,同时该行的结尾也是aicent;也就是本行只有aicent

1)a icent

grep'^a.*t$'  aicent_cm.cfg

查看aicent_cm.cfg文件中有多少个operator(每个operator含有一行PlayerType

bash-3.00# grep "PlayerType"aicent_cm.cfg|wc -l

bash-3.00# grep "^\[" aicent_cm.cfg|wc-l

Vi
统计字符数:
1):%s/^\C/&/g:C代表一个字符, :%s/^\[/&/g:C
2):%s/word//gn: word代表一个字符串, :%s/PlayerType:Operator//gn
vi下全词搜索:
3)/\<findwords\>
vi全局替换:
1):%s/old/new/g     把整个文本中的old替换为new
Sed全局替换:
1)sed "s/old/new/g“

echo $monitorProcessList |sed "s/,/ /g“ > result.log

2)sed -n '3,7p' result.log

huaxi@ubuntu:~/repo$ sudo add-apt-repository ppa:deadsnakes/ppa This PPA contains more recent Python versions packaged for Ubuntu. Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk. Update Note =========== Please use this repository instead of ppa:fkrull/deadsnakes. Reporting Issues ================ Issues can be reported in the master issue tracker at: https://github.com/deadsnakes/issues/issues Supported Ubuntu and Python Versions ==================================== - Ubuntu 20.04 (focal) Python3.5 - Python3.7, Python3.9 - Python3.13 - Ubuntu 22.04 (jammy) Python3.7 - Python3.9, Python3.11 - Python3.13 - Ubuntu 24.04 (noble) Python3.7 - Python3.11, Python3.13 - Note: Python2.7 (focal, jammy), Python 3.8 (focal), Python 3.10 (jammy), Python3.12 (noble) are not provided by deadsnakes as upstream ubuntu provides those packages. Why some packages aren't built: - Note: for focal, older python versions require libssl<1.1 so they are not currently built - Note: for jammy and noble, older python versions requre libssl<3 so they are not currently built - If you need these, reach out to asottile to set up a private ppa The packages may also work on other versions of Ubuntu or Debian, but that is not tested or supported. Packages ======== The packages provided here are loosely based on the debian upstream packages with some modifications to make them more usable as non-default pythons and on ubuntu. As such, the packages follow debian's patterns and often do not include a full python distribution with just `apt install python#.#`. Here is a list of packages that may be useful along with the default install: - `python#.#-dev`: includes development headers for building C extensions - `python#.#-venv`: provides the standard library `venv` module - `python#.#-distutils`: provides the standard library `distutils` module - `python#.#-lib2to3`: provides the `2to3-#.#` utility as well as the standard library `lib2to3` module - `python#.#-gdbm`: provides the standard library `dbm.gnu` module - `python#.#-tk`: provides the standard library `tkinter` module Third-Party Python Modules ========================== Python modules in the official Ubuntu repositories are packaged to work with the Python interpreters from the official repositories. Accordingly, they generally won't work with the Python interpreters from this PPA. As an exception, pure-Python modules for Python 3 will work, but any compiled extension modules won't. To install 3rd-party Python modules, you should use the common Python packaging tools. For an introduction into the Python packaging ecosystem and its tools, refer to the Python Packaging User Guide: https://packaging.python.org/installing/ Sources ======= The package sources are available at: https://github.com/deadsnakes/ Nightly Builds ============== For nightly builds, see ppa:deadsnakes/nightly https://launchpad.net/~deadsnakes/+archive/ubuntu/nightly More info: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa Press [ENTER] to continue or ctrl-c to cancel adding it gpg: keyring `/tmp/tmpcgsx7qod/secring.gpg' created gpg: keyring `/tmp/tmpcgsx7qod/pubring.gpg' created gpg: requesting key 6A755776 from hkp server keyserver.ubuntu.com gpg: /tmp/tmpcgsx7qod/trustdb.gpg: trustdb created gpg: key 6A755776: public key "Launchpad PPA for deadsnakes" imported gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1) OK huaxi@ubuntu:~/repo$ sudo apt update Hit:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu xenial InRelease Hit:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu xenial-updates InRelease Hit:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu xenial-backports InRelease Hit:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu xenial-security InRelease Hit:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial InRelease Reading package lists... Done Building dependency tree Reading state information... Done 142 packages can be upgraded. Run 'apt list --upgradable' to see them. huaxi@ubuntu:~/repo$ sudo apt install python3.6 python3.6-venv Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package python3.6 E: Couldn't find any package by glob 'python3.6' E: Couldn't find any package by regex 'python3.6' E: Unable to locate package python3.6-venv E: Couldn't find any package by glob 'python3.6-venv' E: Couldn't find any package by regex 'python3.6-venv'
最新发布
09-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值