无法将regex与sed匹配 [英] Can't match regex with sed

查看:62
本文介绍了无法将regex与sed匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试匹配模式(\ ^ | \〜?)(\ d | x | \ *)+ \.(\ d | x | \ *)+ \.(\ d |x | \ *)+ sed ,但没有运气.我正在运行的文件是这样的:

I'm trying to match a pattern (\^|\~?)(\d|x|\*)+\.(\d|x|\*)+\.(\d|x|\*)+ with sed without luck. The file I'm running through is this:

{
  "name": "something",
  "version": "0.0.1",
  "description": "some desc",
  "main": "gulpfile.js",
  "directories": {
    "test": "tests"
  },
  "dependencies": {
    "babel-polyfill": "^6.7.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "gulp-clean": "^0.3.2",
    "jquery": "^2.1.4",
    "lodash": "^4.0.0",
    "moment": "^2.13.0",
    "moment-timezone": "^0.5.0",
    "radium": "^0.16.2",
    "react": "^15.1.0",
    "react-bootstrap-sweetalert": "^1.1.10",
    "react-dom": "^15.1.0",
    "react-timeago": "^2.2.1",
    "sprintf": "^0.1.5",
    "smoothscroll": "~0.2.2"
  },
  "devDependencies": {
    "babel": "^6.3.26",
    "babelify": "^7.2.0",
    "browserify": "~12.0.1",
    "console-stamp": "^0.2.0",
    "estraverse-fb": "^1.3.1",
    "gulp": "^3.9.0",
    "gulp-concat": "^2.6.0",
    "gulp-sass": "^2.1.1",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-util": "^3.0.7",
    "lodash": "4.5.1",
    "lodash.assign": "^3.2.0",
    "lodash.isfunction": "^3.0.8",
    "lodash.reduce": "^4.3.0",
    "node-sass": "3.4.2",
    "react-bootstrap": "^0.29.4",
    "react-intl": "2.1.0",
    "reactify": "1.1.1",
    "sweetalert": "^1.1.3",
    "vinyl": "^1.1.0",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0",
    "watchify": "^3.4.0",
    "jsx-to-string": "~0.2.11"
  },
  "optionalDependencies": {
    "pkg-save": "~1.0.2"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "someurl"
  },
  "author": "authorname",
  "license": "MIT"
}

正如您在regexr中看到的,它与所需的模式匹配(也与版本"匹配,但这是我稍后要解决的另一个问题): http://regexr.com/3e324

As you can see in regexr it matches the desired pattern (also matching "version" but that's another issue I'll tackle later): http://regexr.com/3e324

我正在使用以下命令调用sed:
cat package.json |sed's/(\ ^ | \〜?)(\ d | x | \ *)+ \.(\ d | x | \ *)+ \.(\ d | x | \ *)+/Hello/g'-r

I'm invoking invoking sed with the following command:
cat package.json | sed 's/(\^|\~?)(\d|x|\*)+\.(\d|x|\*)+\.(\d|x|\*)+/Hello/g' -r

为简洁起见,它输出类似(即未过滤的输入)的内容:

For the sake of brevity, it outputs something like (ie. unfiltered input):

...
"dependencies": {
    "babel-polyfill": "^6.7.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "gulp-clean": "^0.3.2",
...

应将所有数字替换为"Hello".
我在做什么错?
与错误标志有关(我已经尝试过/gm )
还是没有使用正确的正则表达式引擎(我正在通过 -r 选项来利用扩展的正则表达式)?

It should replace all digits with "Hello".
What am I doing wrong?
Something to do with bad flags (I've tried /gm)
or not using the correct regex engine (I'm passing the -r option to utilize extended regex)?

推荐答案

虽然POSIX正则表达式支持某些命名字符类,例如 [[:digit:]] [[:alnum:]] ,它们不支持速记类,例如 \ d \ w .

While POSIX regular expression support some named character classes, like [[:digit:]] and [[:alnum:]], they do not support shorthand classes such as \d and \w.

一些GNU扩展带来了速记类支持,但仅限于其中的一些, \ w \ W \ s \ S 并根据 regular-expressions.info .

Some GNU extensions bring shorthand classes support, but they are restricted to a few of them, \w, \W, \s and \S according to regular-expressions.info.

通过将正则表达式中的 \ d 替换为 [0-9] ,我得以转换您的文档.正则表达式变为(\ ^ | \〜?)([0-9] | x | \ *)+ \.([0-9] | x | \ *)+ \.([0-9] | x | \ *)+ ,或者更好的 [〜^]([0-9x *] + \.){2} [0-9x *] (感谢Ed Morton!).

By replacing the \d in your regular expression to [0-9] I was able to transform your document. The regular expression becomes (\^|\~?)([0-9]|x|\*)+\.([0-9]|x|\*)+\.([0-9]|x|\*)+, or better [~^]([0-9x*]+\.){2}[0-9x*] (thanks Ed Morton !).

请注意,您的命令可以重写为以下内容,而不使用 cat :

As a side note, your command could be rewritten to the following, which does not use cat :

sed -E 's/[~^]([0-9x*]+\.){2}[0-9x*]/Hello/' package.json

而且正如Matt所言,最好使用JSON解析器,例如 jq .

And as noted by Matt, you'd be better off using a JSON parser such as jq.

这篇关于无法将regex与sed匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆