0

Hello and excuse me again

I need a REGEX for delete the character space (" "). If this character is not followed for other space character.

Example String

"This  is  a  ex am ple" 

Result 

"This  is  a  example"

I was trying this REGEXES:

string.gsub(/\s{1}/,"")

Also I thought in submatches replace for I don´t know as use it in ruby

string.gsub(/(\w)( )(\w)/,/\1\3/)

However throw a exception.

Thanks in advance

4 Answers 4

4

try with this:

print "This  is  a  ex am ple".gsub(/(?<=\w)\s(?=\w)/, '')

output:

This  is  a  example
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think (?<=\S)\s(?=\S) is a good idea. It maybe combine non-alphabet & alphabet together. Sorry PraVn, I change it back.
1

You have small error in second gsub. Try this:

string.gsub(/(\w)( )(\w)/, '\1\3')

I checked this on your example, it works for me.

1 Comment

Thank you very much. I use your solution
0
string.gsub( /(\w) (\w)/, '\1\2' )

What do you want to happen to a space at the start?

Comments

0

Try this reg expression:

string.gsub(/(?<!\s)\s(?!\s)/, '');

Input:

"This     is    a  ex am ple"

Output:

"This     is    a  example"

2 Comments

Excuse me The Regex in my opinion is correct. However when I try this regex I obtain -> "This is a ex am ple" (Excuse me I obtain same result)
I misunderstood your question, now i got it. I modified the answer

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.