Find and replace the special character and special character with character

advertisements

I would like to to modify given strings:

a = "John;Rich;[email protected]\r\n"
b = "John;Rich;[email protected]\r"
c = "John,Rich,[email protected]\n"

To one format:

"xxx,yyy,zzzz\n"

I think the best way to do that is by using regex to find and replace, but I have no experience with it. I wrote simple code to change ; => , :

a.gsub(/[;]/,',')

I figured out that regex /(\\r\\n)/ will find for me \r\n and /(\\r)/ - \r.
I have a problem with joining all regexes together to perform whole string modification with one gsub.


Try this:

result = subject.gsub(/[\r\n]+/, '\n')

RegEx Anatomy:

"
[\\r\\n]    # Match a single character present in the list below
             # A carriage return character
             # A line feed character
   +         # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"

Hope this helps...