• 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端
所有文章 关于我

  • 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端

Shell三连(二):如何使用正则获取变量

2018-12-02

心路历程

在shell的正则表达式里常用的有

  1. sed(stream editor)
  2. grep(file pattern searcher)
  3. BASH_REMATCH(Bash3.x自带表达式 =~ )

值得注意的是以上三种都不支持非贪婪模式(non-greedy)的匹配,因为无法对一行字符串进行多次匹配,参考regex in bash expression

Example

我们假设摘取以下字符串里的key值 some_important_string

1
2
string="HEREISKEY some_important_string HEREISVALUE some_value"

sed

sed的使用方法比较特别,需要遵守 s/regexp/replacement/flags 的使用模版参考文档

  1. \1 表示("some_(.+)") 匹配的内容
  2. \2 表示(.+)匹配的内容

代表将表达式 “^.* ("some_(.+)") .*” 匹配到的字符替换为 “\1” 也就是 “("some_(.+)")” 并输出

1
2
echo "HEREISKEY \"some_important_string\" HEREISVALUE \"some_value\"" | sed -E "s/^.* (\"some_(.+)\") .*/\1/"

grep

grep不像sed一样可以使用 -E 参数来表达正则,而不用对大部分符号使用转义符

1
2
echo "HEREISKEY \"some_important_string\" HEREISVALUE \"some_value\"" | grep -o -E "\"some_(.+)\"\s"

BASH_REMATCH

该方法为自带表达式 推荐使用这一种 其思想和 sed一样,是按照顺序将结果放入数组

| 数组下标 | 匹配表达式 | 匹配结果 |
| :————- | :————- |
| BASH_REMATCH[0] | ^.( "some_(.+)" ). | HEREISKEY “some_important_string” HEREISVALUE “some_value” |
| BASH_REMATCH[1] | ( "some_(.+)" ) | “some_important_string” |
| BASH_REMATCH[2] | (.+) | important_string |

1
2
if [[ "HEREISKEY \"some_important_string\" HEREISVALUE \"some_value\"" =~ ^.*( \"some_(.+)\" ).* ]] ; then echo ${BASH_REMATCH[1]}; fi

三者区别

命令 思想
sed、BASH_REMATCH 先匹配全文,然后将自表达式匹配结果依次放入index
grep 直接截取表达式匹配到的部分,并不会把自表达式放入index
赏

请问老板还招人么(/ω\)

支付宝
微信
  • Shell
  • Tips

扫一扫,分享到微信

微信分享二维码
Shell三连(三):文件的读写和重定向
Shell三连(一):脚本参数设定和解析
© 2021 Alan Li
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • iOS
  • Java
  • Collection
  • Python
  • Shell
  • CMake
  • Memory
  • JavaScript
  • Architecture
  • AnchorPoint
  • Android
  • Web
  • Annotation
  • AFNetworking
  • Window
  • ViewController
  • AutoLayout
  • Dozer
  • CoreAnimation
  • Cycle Retain
  • Block
  • UI
  • IDE
  • FrontEnd
  • CSS
  • Category
  • TableViewCell
  • Security
  • Net
  • JSP
  • Spring
  • C
  • MyBatis
  • Date
  • React
  • GCD
  • UITouch
  • Gesture
  • UIControl
  • Git
  • HTML
  • HTTPS
  • HTTP
  • Servlet
  • Server
  • DataBase
  • MySQL
  • Linux
  • Tutorial
  • Ajax
  • Type
  • JQuery
  • JSON
  • Exception
  • Parameter
  • Reflect
  • Thread
  • Sort
  • KVO
  • MKMapKit
  • Overlay
  • Maven
  • Configure
  • Tips
  • Transaction
  • Swift
  • NavigationBar
  • Nginx
  • Runtime
  • OpenCV
  • Property
  • Playground
  • Protocol
  • Redux
  • ScrollView
  • Session
  • Cookie
  • Shiro
  • Error
  • Singleton
  • RegEx
  • StackView
  • StatusBar
  • Base64
  • Socket
  • TCP
  • IP
  • TextField
  • CALayer
  • UILabel
  • View
  • Animation
  • Xcode
  • Hexo
  • Terminal
  • OC
  • Device
  • Log
  • Image
  • JUnit
  • Oval
  • Archive
  • XSS
  • Compiler
  • Aspect
  • Responder
  • Class
  • FireWall
  • RetainCount
  • Const
  • Frame
  • String
  • Symbols
  • Framework
  • CocoaPods
  • Unity
  • Message
  • Button
  • AuthorizationStatus
  • Struct
  • XCTest
  • NSNotification
  • Contact

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

我写的,大概率是错的。。。。。