locked
Regex Expression to Replace Escaped Whitespace RRS feed

  • Question

  • (This is a Regular Expression question.  I know that there used to be a forum devoted to Regular Expressions but I find this new organization of the forums confusing and could not figure out how to post this in the Regular Expressions forum (if it still exists).

    I am working on an app to assist in the evolution of some html text to a pattern.  One feature I want to provide is replacing one or more whitespace characters between tags with \s*.  But the html may or may not already have been escaped (the code knows whether it has been escaped or not).  When the html has not been escaped this works:

    ansstr &= m.Groups("starttag").Value & Regex.Replace(g.Value, "\s+", "\s*") & m.Groups("endtag").Value

    But I haven't found a way to replace escaped whitespace.  I've tried both of the following:

    ansstr &= m.Groups("starttag").Value & Regex.Replace(g.Value, "(\\[ trnvf]+)", "\s*") & m.Groups("endtag").Value

    ansstr &= m.Groups("starttag").Value & Regex.Replace(g.Value, "\\[ trnvf]+", "\s*") & m.Groups("endtag").Value

    But in both cases each pair of characters representing an escaped whitespace character (e.g. \n) is replaced by \s* (resulting in, e.g.,  \s*\s*\s*\s*\s*).  What I want to do is replace the entire string of escaped whitespace characters, e.g., \n\t\t\t\t\t, with the three characters \s*.

    I don't think that this is a difficult problem but I am just not seeing it.

    Thanks,  Bob


    Sunday, September 8, 2013 11:04 PM

Answers

  • Try this sequence too: “(\s|\\[ trnvf])+”.

    Monday, September 9, 2013 7:26 PM