Regex headache..
-
Hi all, I have been trying this for a while (but I've never really quite got the hang of regular expressions, they give me a headache!), but I can't seem to figure it out. Could anyone provide me with a pointer or two (or an answer would be fabulous). I have some strings coming from our db. I want to replace some sections. For example here's a string: "… Jack Frost nipping at your nose... a load more text goes here" I want to insert a line break after the "…" instead of having "… ", however, I don't want to replace the first instance of "… " as that's how the string starts.
string regex1 = "(.)";
string regex2 = "(\u2026)";
string regex3 = "(\\s+)";using
Regex.replace(string, regex1 + regex2 + regex3, "\u2026
, RegexOptions.IgnoreCase)gets me tantelisingly close, but obviously chops off the first letter before the "…" (if there is one). leaving me with: "… Jack Frost nipping at your nos…
a load more text goes here" How do I do the expression so that its not looking for characters before the "…", but looking for the begining of the string? Thanks in advance for the help.... Mark (and yes, I have tried putting a line break in the string in the db, but am being told that's not alowed...) -
Hi all, I have been trying this for a while (but I've never really quite got the hang of regular expressions, they give me a headache!), but I can't seem to figure it out. Could anyone provide me with a pointer or two (or an answer would be fabulous). I have some strings coming from our db. I want to replace some sections. For example here's a string: "… Jack Frost nipping at your nose... a load more text goes here" I want to insert a line break after the "…" instead of having "… ", however, I don't want to replace the first instance of "… " as that's how the string starts.
string regex1 = "(.)";
string regex2 = "(\u2026)";
string regex3 = "(\\s+)";using
Regex.replace(string, regex1 + regex2 + regex3, "\u2026
, RegexOptions.IgnoreCase)gets me tantelisingly close, but obviously chops off the first letter before the "…" (if there is one). leaving me with: "… Jack Frost nipping at your nos…
a load more text goes here" How do I do the expression so that its not looking for characters before the "…", but looking for the begining of the string? Thanks in advance for the help.... Mark (and yes, I have tried putting a line break in the string in the db, but am being told that's not alowed...)regex's defeat me too, I must admit... however... why can't you have line breaks in the db? Are "they" saying you can't insert html tags? In which case you could insert CR/LF characters instead, which would be better and would at least allow for better import into a textarea, for example... but whether or not, do you have to use regex? Can't you use the String.Replace function instead? str = str.Substring(0,2) & str.Substring(2).Replace("..", "
") -
regex's defeat me too, I must admit... however... why can't you have line breaks in the db? Are "they" saying you can't insert html tags? In which case you could insert CR/LF characters instead, which would be better and would at least allow for better import into a textarea, for example... but whether or not, do you have to use regex? Can't you use the String.Replace function instead? str = str.Substring(0,2) & str.Substring(2).Replace("..", "
")Well... It's my boss who isn't letting me put linebreaks etc in the db, so I don't have any leeway there... I did have a good try at string.replace() but got a bit confused with first instances of etc. and so thought maybe a regex may be a bit cleaner. I have litterally just come up with a simple solution, though it isn't ideal, it does work. I'm using this now...
string regex1 = "\\A+"; //matches begining of string
string regex2 = "(\u2026)"; //matches … char
string regex3 = "(\\s+)"; //matches a space char
string regex4 = @"( <br /> )"; // matches <br />OfferDescriptionStr = Regex.Replace(OfferDescriptionStr, regex2 + regex3, "\u2026<br />", RegexOptions.IgnoreCase);
OfferDescriptionStr = Regex.Replace(OfferDescriptionStr, regex1 + regex2 + regex4, "\u2026 ", RegexOptions.IgnoreCase);Basically, as you can see, I gave up looking for 'Not the begining of a string', replcaed all instances of "… " with "…<br />", then... looked for the begining of the string and replaced "…<br />" with "… " as it was originally. A Bit messy but it works a treat now. Thanks for the response, if time allows (though I doubt it), I might well try again on the string.replace(). Maybe at the start of the day before I get too bogged down in it again.. Cheers Mark