sed的s / a * //为什么不匹配所有a?

时间:2018-08-20 16:41:51

标签: regex sed

此sed替换的原因是什么

echo "xaaa" | sed  's/a*//'

产生xaaa。它不是要用任何东西代替任何连续的a吗?

3 个答案:

答案 0 :(得分:5)

除非您在最后一个g之后添加/(对于Global),否则sed只会替换第一个匹配项。

您的a*正则表达式匹配一个空字符串(*表示零或多个),因此它将x之前的空字符串替换为另一个空字符串,然后停止。

答案 1 :(得分:3)

这是因为watch意味着var gulp = require('gulp'); var browserSync = require('browser-sync').create(); var cp = require('child_process'); var pug = require('gulp-pug'); var sass = require('gulp-sass'); var prefix = require('gulp-autoprefixer'); var minifycss = require('gulp-minify-css'); var bourbon = require('bourbon').includePaths; var rename = require('gulp-rename'); // Message var messages = { jekyllBuild: '<span style="color: white">Running:</span> $ jekyll build' }; // Paths var paths = { html: { src: ['*.html', '_layouts/*.html', '_posts/*', '_includes/*'], dest: '_includes' }, pugFiles: { src: '_jadefiles/*.jade', dest: '_includes' }, styles: { all: 'assets/css/**', src: 'assets/css/main.scss', dest: ['assets/css', '_site/assets/css'] }, scripts: { src: ['assets/js/**/*.js', 'js/*.js'], dest: ['js', '_site/assets/js'] } } // Compile Pug|Jade files into .html gulp.task('pug', function() { return gulp.src(paths.pugFiles.src) .pipe(pug()) .pipe(gulp.dest(paths.pugFiles.dest)); }); // Compile sass into CSS & auto-inject into browsers gulp.task('sass', function() { return gulp.src(paths.styles.src) .pipe(sass({ includePaths: [bourbon], onError: browserSync.notify }).on('error', sass.logError)) .pipe(gulp.dest(paths.styles.dest)) .pipe(prefix({ browsers: ['last 15 versions', '> 1%', 'ie 8', 'ie 7'], cascade: true })) .pipe(rename({suffix: '.min', prefix : ''})) .pipe(minifycss()) .pipe(browserSync.stream()); }); // Watch changes on files gulp.task('watch', function() { // gulp.watch(paths.scripts.src).on('change', browserSync.reload); gulp.watch(paths.html.src, gulp.series('jekyll-rebuild')); gulp.watch(paths.pugFiles.src, gulp.series('pug')); gulp.watch(paths.styles.all, gulp.series('sass')); gulp.watch(paths.html.dest).on('change', browserSync.reload); }); // Build Jekyll gulp.task('jekyll-build', function (done) { browserSync.notify(messages.jekyllBuild); var pl = process.platform === "win32" ? "jekyll.bat" : "jekyll"; return cp.spawn(pl, ['build'], {stdio: 'inherit'}) .on('close', done); }); // Rebuild Jekyll site gulp.task('jekyll-rebuild', gulp.series('jekyll-build', function () { browserSync.reload(); }) ); // Static Server gulp.task('serve', function() { browserSync.init({ server: { baseDir: '_site' } }); }); gulp.task('default', gulp.series('serve', gulp.parallel('sass', 'pug', 'watch', 'jekyll-build'))); 或更多匹配项,因此它匹配一个空字符串,并且不替换任何内容。

但是,如果您添加*(全局)标志,则它将替换所有0

g

答案 2 :(得分:1)

如果使用全局替换,则无需使用*运算符:

echo "xaaa" | sed 's/a//g'
x

g标志可确保替换所有出现的模式。