4

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!

1
  • What about ordinary spaces? Do you also want to replace two spaces with one space? Commented Jan 27, 2010 at 9:31

6 Answers 6

4

s/\s+/ /g

s/(?:\\[rnt])+/ /g
Sign up to request clarification or add additional context in comments.

6 Comments

@neezer, note that \s matches more than just \t, \r or \n. It matches (in most cases) these: [ \t\n\x0B\f\r], but that may be fine with you of course.
@neezer: What language are you using?
@neezer: In what way doesn't it work? Does it give an error? Does it replace too much? Too little? Does it do nothing at all?
I'm using Ruby. It doesn't match anything. The values in the string in my question are not escape values (as in, to match, you'd search for \\r or the like).
Your edit matches them individually, not the group. I need to replace the whole group with a single space. Any other thoughts?
|
0

In PHP:

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

2 Comments

did you see this was a ruby question?
When I answered there was no mention of ruby, or any other language. The post was edited later, as you can see. Also, accepted answer uses Perl's syntax.
0
sed 's/\\[rnt]/ /g;s/  */ /g'

Comments

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

Comments

0
#!/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"

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.