Regex: Match this string
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+/, ' ')
精彩评论