Perl& Sed字符串替换了几个表达式

时间:2015-07-29 02:51:08

标签: regex perl sed

我想以非贪婪的比赛方式进行字符串替换

  • 删除所有前导和尾随破折号,撇号(当在单词的中间找到这些符号时,必须保留它们)

  • 将多个空格转换为1个空格

示例:

--ONE   Tw'o--   -333-   -'FO-UR'

必须成为

ONE Tw'o 333 FO-UR

我无法得到确切的结果。你能帮我修正下面的perl和sed语法吗?

$ echo "--ONE   Tw'o--   -333-   -'FO-UR'" \
  | perl -pe "s/[-']+(.+?)/\1/g"           \
  | perl -pe "s/(.+?)[-']+/\1/g"           \
  | perl -pe "s/\s+/ /g"

Result (perl): "ONE Two 333 FOUR"

$ echo "--ONE   Tw'o--   -333-   -'FO-UR'" \
  | sed -r -e "s/[-']+(.+?)/\1/g"          \
    -e "s/(.+)[-']+/\1/g"                  \
    -e "s/\s+/ /g"

Result (sed): "ONE Tw'o-- -333- -'FO-UR"

2 个答案:

答案 0 :(得分:3)

这是perl版本:

echo "--ONE   Tw'o--   -333-   -'FO-UR'" | perl -ne "s|-'||g; s|'-||g; s|^'||; s|'$||; s|^-+||; s|-+$||; s|-+\s+| |g; s|\s+-+| |g; s|\s+| |g; s|\s+$||; print;"

ONE Tw'o 333 FO-UR

sed版本基本相同:

echo "--ONE   Tw'o--   -333-   -'FO-UR'" | sed -r -e "s|-'||g; s|'-||g; s|^'||; s|'$||; s|^-+||; s|-+$||; s|-+\s+| |g; s|\s+-+| |g; s|\s+| |g; s|\s+$||;"

ONE Tw'o 333 FO-UR

使用的正则表达式的注释:

s|-'||g;     # Remove dash followed by quote everywhere
s|'-||g;     # Remove quote followed by dash everywhere
s|^'||;      # Remove leading quote
s|'$||;      # Remove trailing quote
s|^-+||;     # Remove leading dash characters
s|-+$||;     # Remove trailing dash characters
s|-+\s+| |g; # Replace dash characters followed by whitespace with 1 space everywhere
s|\s+-+| |g; # Replace whitespace followed by dash characters with 1 space everywhere
s|\s+| |g;   # Replace multiple spaces with 1 space
s|\s+$||;    # Remove trailing spaces

答案 1 :(得分:3)

使用SELECT COUNT(DISTINCT Project), COUNT(DISTINCT Node) FROM YourTable 中的外观很容易:

perl