Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Regex headache..

Regex headache..

Scheduled Pinned Locked Moved Web Development
questiondatabaseregexhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    markymark82
    wrote on last edited by
    #1

    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...)

    N 1 Reply Last reply
    0
    • M markymark82

      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...)

      N Offline
      N Offline
      NeverHeardOfMe
      wrote on last edited by
      #2

      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("..", "
      ")

      M 1 Reply Last reply
      0
      • N NeverHeardOfMe

        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("..", "
        ")

        M Offline
        M Offline
        markymark82
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups