巴什普通防爆pression - 似乎无法匹配\\ S \\ S,等 [英] Bash Regular Expression -- Can't seem to match \s, \S, etc

查看:94
本文介绍了巴什普通防爆pression - 似乎无法匹配\\ S \\ S,等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正试图从获得的GParted的信息块的脚本。

I have a script that is trying to get blocks of information from gparted.

我的数据是这样的:

Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  316MB   315MB   primary  ext4            boot
 2      316MB   38.7GB  38.4GB  primary  ext4
 3      38.7GB  42.9GB  4228MB  primary  linux-swap(v1)

log4net.xml
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  316MB   315MB   primary  ext4            boot
 5      316MB   38.7GB  38.4GB  primary  ext4
 6      38.7GB  42.9GB  4228MB  primary  linux-swap(v1)

我用正则表达式来分成两个磁盘块这种

I use a regex to break this into two Disk blocks

^磁盘(/ dev的[\\ S] +):((?!磁盘)\\ S \\ S])*

^Disk (/dev[\S]+):((?!Disk)[\s\S])*

这适用于对多行。

当我测试这在bash脚本,我似乎无法匹配\\ S,或用\\ s? - 什么我做错了。

When I test this in a bash script, I can't seem to match \s, or \S -- What am I doing wrong?

我是通过像一个测试脚本这样的:

I am testing this through a script like:

data=`cat disks.txt`
morematches=1
x=0
regex="^Disk (/dev[\S]+):((?!Disk)[\s\S])*"

if [[ $data =~ $regex ]]; then
echo "Matched"
while [ $morematches == 1 ]
do
        x=$[x+1]
        if [[ ${BASH_REMATCH[x]} != "" ]]; then
                echo $x "matched" ${BASH_REMATCH[x]}
        else
                echo $x "Did not match"
                morematches=0;
        fi

done

fi

然而,当我穿行于正则表达式,每当我匹配\\ s或\\ S的测试部分,这是行不通的 - 我在做什么错

However, when I walk through testing parts of the regex, Whenever I match a \s or \S, it doesn't work -- what am I doing wrong?

推荐答案

也许\\ S和\\ s没有支持,或者,你不能把它们周围的 [] 。尝试使用这种格式来代替:

Perhaps \S and \s is not supported or that you can't place them around [ ]. Try to use this format instead:

^Disk[[:space:]]+/dev[^[:space:]]+:[[:space:]]+[^[:space:]]+

修改

看来你真的想要得到的匹配字段。我做了简单的脚本来这一点,但我不知道如果这是你想要的输出:

It seems you actually want to get the matching fields. I made the script simpler to this but I'm not sure if it's your intended output:

#!/bin/bash 

regex='^Disk[[:space:]]+(/dev[^[:space:]]+):[[:space:]]+(.*)'

while read line; do
    [[ $line =~ $regex ]] && echo "${BASH_REMATCH[1]} matches ${BASH_REMATCH[2]}."
done < disks.txt

将会产生

/dev/sda matches 42.9GB.
/dev/sdb matches 42.9GB.

这篇关于巴什普通防爆pression - 似乎无法匹配\\ S \\ S,等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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