Replacing String Using Regular Expression

Discussion in 'ASP .Net' started by lucky, Oct 22, 2005.

  1. lucky

    lucky Guest

    hi,
    i got file which contains "----------------" in a line. the line only
    contains this data as a saperation. using regular expression i want to
    i detify the line contains that data and replace with spaces.
    if anyone has any idea,solution or link plz do share with me.

    thans in advt.

    Lucky
    lucky, Oct 22, 2005
    #1
    1. Advertising

  2. Lucky,

    Is there any reason that you want to do this with a Regular Expression and
    not with a normal replace command?

    Cor
    Cor Ligthert [MVP], Oct 22, 2005
    #2
    1. Advertising

  3. Hi Lucky,

    if your separtor length is always the same this
    regex will work for you.

    string content = null;
    using (StreamReader sr = new StreamReader("Test.txt"))
    {
    String line = null;
    while ((line = sr.ReadLine()) != null)
    {
    //if seperator line length 10
    Regex rgx = new Regex("^-(-){8}-$");
    if(rgx.IsMatch(line.Replace(@"\n","")))
    continue;
    content += line;
    }
    }

    Cheers
    Lars Behrmann

    _________________
    Nothing is impossible. UML is the key for all your problems.
    AODL - Make your .net apps OpenOffice ready
    http://aodl.sourceforge.net/
    lucky schrieb:

    > hi,
    > i got file which contains "----------------" in a line. the line only
    > contains this data as a saperation. using regular expression i want to
    > i detify the line contains that data and replace with spaces.
    > if anyone has any idea,solution or link plz do share with me.
    >
    > thans in advt.
    >
    > Lucky
    Lars Behrmann, Oct 22, 2005
    #3
  4. "lucky" <> schrieb:
    > i got file which contains "----------------" in a line. the line only
    > contains this data as a saperation. using regular expression i want to
    > i detify the line contains that data and replace with spaces.


    I think that regular expressions are far oversized for this problem
    (interesting article on this topic:
    <URL:http://msmvps.com/jon.skeet/archive/2005/09/21/67247.aspx>). Check out
    both the methods of the 'String' class and the members of the 'Strings'
    module, more specific the 'Replace' methods.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>
    Herfried K. Wagner [MVP], Oct 22, 2005
    #4
  5. If the string is always the same, the same characters, and length, there is
    no need for a Regular Expression. Regular Expressions look for patterns. You
    are looking for a specific substring. Use String.Replace.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Ambiguity has a certain quality to it.

    "lucky" <> wrote in message
    news:...
    > hi,
    > i got file which contains "----------------" in a line. the line only
    > contains this data as a saperation. using regular expression i want to
    > i detify the line contains that data and replace with spaces.
    > if anyone has any idea,solution or link plz do share with me.
    >
    > thans in advt.
    >
    > Lucky
    >
    Kevin Spencer, Oct 22, 2005
    #5
  6. lucky

    Lucky Guest

    well my problem is that, i dont know the size of the string. it could
    be 80 or 800 or 8000. main purpose of the line is to saperate data
    lines. but i'm not getting how can i replace such a big line in one
    shot though i've done work around but if still some one can help me to
    find a short way of replacing a whole line would be appriciated.

    Lucky
    Lucky, Oct 24, 2005
    #6
  7. My problem is that I can't tell from your message what the context of it is.
    My newsreader hides read messages by default, and I really don't like having
    to switch back and forth from hding to viewing to hiding again. It requires
    a lot of trouble and time to do so. Not so much for a single thread, but
    multiply that by the number of messages and threads I read every day, and
    you have a significant waste of time, which is unfortunately, quite limited
    to me, and others who are working at successful jobs.

    Now, this is fairly common among people who regularly use newsgroups. And if
    you make it more difficult for someone to figure out what you're asking, as
    time is limited, many people will simply go on to another question that
    requires less time and trouble to answer. So, if I have a question, I want
    to make sure that as many people as possible, who may be able to answer it,
    will answer it. This means that I do everything I can to make them work
    less. I include as many facts and data about a problem as I can. If I am
    continuing a thread, I will quote the necessary previous messages in the
    thread as contribute to the facts needed to solve the problem.

    Think of it like fishing. There are only so many fish in the pond. You want
    to use bait that will attract as many as possible. Hopefully, one of them
    will be the big fish you're looking to catch.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    A watched clock never boils.

    "Lucky" <> wrote in message
    news:...
    > well my problem is that, i dont know the size of the string. it could
    > be 80 or 800 or 8000. main purpose of the line is to saperate data
    > lines. but i'm not getting how can i replace such a big line in one
    > shot though i've done work around but if still some one can help me to
    > find a short way of replacing a whole line would be appriciated.
    >
    > Lucky
    >
    Kevin Spencer, Oct 24, 2005
    #7
  8. Lucky,

    You can try this, however, check that this is possible in your situation.

    \\\
    Dim str As String = "-----------------------"
    str = str.Replace("--", " ")
    str = str.Replace(" -", " ")
    ///
    I hope this helps,

    Cor
    Cor Ligthert [MVP], Oct 24, 2005
    #8
  9. lucky

    Greg Bacon Guest

    In article <>,
    lucky <> wrote:

    : i got file which contains "----------------" in a line. the line only
    : contains this data as a saperation. using regular expression i want to
    : i detify the line contains that data and replace with spaces.
    : if anyone has any idea,solution or link plz do share with me.

    Does the code below help?

    static void Main(string[] args)
    {
    string[] inputs = new string[]
    {
    "Every", "-", "Good", "--", "Boy", "---",
    "Does", "----", "Fine", "--------------",
    };

    Regex alldashes = new Regex(@"^(-+)$");
    MatchEvaluator zap = new MatchEvaluator(BlankDashes);
    foreach (string line in inputs)
    Console.WriteLine(
    "line = [" + alldashes.Replace(line, zap) + "]");
    }

    private static string BlankDashes(Match m)
    {
    return m.Groups[1].Value.Replace("-", " ");
    }

    Greg
    --
    Comfort rides shotgun with the temporary.
    -- Daniel Weinshenker
    Greg Bacon, Oct 24, 2005
    #9
  10. lucky

    Lucky Guest

    thanks to all you guys. b'coz of your help i've solved the problem in
    both ways. by regular expression(i learnt a lot) and with string member
    functions(now i got some more mastery on those functions).
    Lucky, Oct 25, 2005
    #10
  11. Can you please post the code you ended up using for both approaches? For
    others with the same problem it will be helpful to see both solutions
    together in this post.

    Just in general it is nice to be able to see how problems were solved.


    "Lucky" <> wrote in message
    news:...
    > thanks to all you guys. b'coz of your help i've solved the problem in
    > both ways. by regular expression(i learnt a lot) and with string member
    > functions(now i got some more mastery on those functions).
    >
    Jim Underwood, Nov 3, 2005
    #11
  12. lucky

    Lucky Guest

    sorry dude but after i solved the problem my project manager had
    scraped the solution and i dont have any backup of solution but go
    through the thread discussion coz i had developed the solution on the
    basis of the discussion. pleople had helped me a lot so i think same
    thing will work for you. you have to taken pain to read all postings
    Lucky, Nov 11, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,272
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    832
    Alan Moore
    Dec 2, 2005
  3. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    260
    Chris Hohmann
    Apr 11, 2005
  4. Ben
    Replies:
    4
    Views:
    131
    Robert Klemme
    Mar 25, 2008
  5. Zeno Davatz
    Replies:
    5
    Views:
    124
    Robert Klemme
    Feb 22, 2011
Loading...

Share This Page