开发者

Regex: Match this string

开发者 https://www.devze.com 2022-12-18 01:04 出处:网络
I can\'t figure this out: 22.584\\r\\n\\t\\t\\tl-6.579-22 I want to match the \"\\r\\n\\t\\开发者_如何学运维t\\t\" and replace with a single space \" \". Problem is the number of \"\\t\", \"\\r\",

I can't figure this out:

22.584\r\n\t\t\tl-6.579-22

I want to match the "\r\n\t\开发者_如何学运维t\t" and replace with a single space " ". Problem is the number of "\t", "\r", and "\n" fluctuates, as do the surrounding characters.

Help!


s/\s+/ /g

s/(?:\\[rnt])+/ /g


In PHP:

preg_replace("/(?:\\\[trn])+/", " ", $str);


sed 's/\\[rnt]/ /g;s/  */ /g'


'22.584\r\n\t\t\tl-6.579-22'.gsub(/(\\[rnt])+/, ' ')


#!/usr/bin/ruby1.8

s = "22.584\r\n\t\t\tl-6.579-22"
p s                           # => "22.584\r\n\t\t\tl-6.579-22"
p s.gsub(/[\r\n\t]+/, ' ')    # => "22.584 l-6.579-22"


I'd treat the CR-NL as one atom:

str.gsub!(/(?:\r\n)+\t+/, ' ')
0

精彩评论

暂无评论...
验证码 换一张
取 消