Re: gsub and regex



Stefano Crocco wrote:
On Wednesday 18 June 2008, Tom Cloyd wrote:

I'm trying to replace all /\n / with /\n/ in a string, but gsub won't
allow a regex as the replacement when a match occurs. I'm getting myself
considerably confused about data types and regex, and just plain need
some help. Is there some way to do this replacement, using gsub?

Thanks,

t.


I'm not sure I understand you. Do you want to replace all newline charactes
followed by a space with a single newline character? If so, you don't need
regexps at all:

str.gsub("\n ", "\n")

I hope this helps

Stefano




Man, this list is absolutely amazing. Here I am programming at 4:30 in
the morning, and get stuck, and before I can execute my program again, I
get my problem solved. I think I want to live in Ruby land the rest of
my life.

And...how could I have forgotten that in "...." \n get understood. Jeez.
So easy.

Thanks for being there.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@xxxxxxxxxxxx >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


.



Relevant Pages

  • Re: gsub and regex
    ... allow a regex as the replacement when a match occurs. ... considerably confused about data types and regex, ... Do you want to replace all newline charactes ...
    (comp.lang.ruby)
  • gsub and regex
    ... allow a regex as the replacement when a match occurs. ... considerably confused about data types and regex, and just plain need ...
    (comp.lang.ruby)
  • Re: Replace character
    ... Capture the parts you want to keep in a named group and put them back into the replacement pattern $. ... looks back in your string and tries to find the pattern that is defined at the '...'. ... The funny thing is, that even though you search for something in your regex, it doesn't become part of the actual Match and therefore doesn't get replaced. ... Second option is to use verbatim strings in C# ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Replace character
    ... Regex.Replace with a reasonable regex expression for valid email ... addresses should give a reasonable correct replacement. ... The second problem is that this identifies all emails on my string ... Second option is to use verbatim strings ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Additional Functions for base classes.
    ... Unless you want to replace them all at the same time, using Regex is a very expensive operation to do single string replacements. ... You can use a regex to specify all of the characters that need to have somthign prefixed by grouping them into a characterset. ... The replacement patterns are a little different than you're used to, because it is possible to include the text you originally found. ...
    (microsoft.public.dotnet.languages.csharp)