简体   繁体   English

正则表达式:匹配多个空格不起作用

[英]Regex: Matching multiple spaces doesn't work

I have this content in file: 我在文件中有此内容:

  core        = 7.x
  base theme  = adaptivetheme
  release     = 7.x-3.x
  engine      = phptemplate

I'm tring to get everything after: base theme = to the end of line, so I would like to get adaptivetheme . 我想在此之后获得所有信息: base theme =至行尾,因此我想获得adaptivetheme

Based on https://unix.stackexchange.com/a/24151/37309 I'm trying to do: 基于https://unix.stackexchange.com/a/24151/37309我正在尝试做:

sed -n 's/base\stheme\s+=\s//p' file
sed -n 's/base\stheme += //p' file

But it doesn't work. 但这是行不通的。 However this does work: 但这确实有效:

sed -n 's/base\stheme  = //p' file

So what I'm doing wrong? 所以我做错了什么? I would like to match it regardless of number of spaces that there can be. 无论空间有多少,我都想匹配它。

Use the -r -flag for extended regex-support: 使用-r -flag扩展正则表达式支持:

sed -r 's/base\stheme\s+=\s//'

在此处输入图片说明

You can use 您可以使用

sed -n 's/base theme \+= //p' file

This says "one or more spaces" (that's the meaning of the escaped \\+ ). 这表示“一个或多个空格”(这是转义的\\+的含义)。

By itself, sed uses only the basic Regex (BRE). sed本身仅使用基本的Regex(BRE)。 If you want to use extended regex some rules change - like what characters need to be escaped or not. 如果您想使用扩展的正则表达式,则某些规则会发生变化-例如哪些字符需要转义或不转义。 You can invoke the extended mode with the -r flag. 您可以使用-r标志调用扩展模式。 Then the + no longer needs to be escaped. 然后+不再需要转义。

These dialects of regex are very confusing. 这些正则表达式的方言非常令人困惑。 I keep having to look it up myself, too... 我也要自己查一下...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM