Search and insert a string in a file using regex

Locked Search and insert a string in a file using regex

  • Wednesday, February 01, 2012 7:49 AM
     
     

    Hello all,

    I want to verify a file so that no nested braces should be present in that file. If present it is supposed that user forgot to give closing brace(i.e. file is not correct). And my code should insert a closing brace after the last occurrence of semicolon. Like the following: If the file contains

    abc{def;ghe;ijk{lmn}

    Then this should be after correction:            abc{def;ghe;}ijk{lmn}

    Problem1: My code for verification of the file is like

    Regex regex = new Regex(@"\{(.*?[^{][^}])\{");

    Match match = regex.Match(streamReader.ReadToEnd());

                    if (match.Success)

    //report error and correct the file

    This is working for single line statement(i.e. if "{def;ghe;ijk{" is in one line or in the next line(i.e. "{def;ghe;ijk" is in one line and "{" is on the next line)). But if i give 2 line breaks between ghe;ijk and { then the pattern is not matching.

    Help me find the corrected regex.

    Problem2: How can I insert a closing brace after the last occurrence of a semicolon in the matched pattern and write it into the same file? I want to do it using regex. I mean,the steps will be like: getting the content between 2 opening braces which doesn't contain any opening or closing braces. And then the content will be checked for the last occurrence of semicolon and add a closing brace after it.

    Please Help me out !!!

All Replies

  • Monday, February 13, 2012 5:41 AM
     
     
    I have same proble also. please suggest if anyone has an idea or solution for the same.
  • Wednesday, February 15, 2012 4:28 PM
     
     

    Hi,

    for the multiline problem try using RegexOptions.Multiline on the constructor of the Regex class while for replace you can use grouping


    Bilhan silva

  • Monday, February 20, 2012 3:35 PM
     
     Answered

    Hi,

    not well tested, but it might gives you a start

    (?<=
        {
        (?:
            [^};]|
            (?(sem)
                ;|
                (?<sem>;)
            )
        )+
    )
    {

    It searches for "{" (the last character of the expression). and looks, if what there's before (?<=). Use a @"verbatim"-string and the RegexOption IgnorePatternWhitespace (so you have not to condense the pattern). Since there's no "." in the pattern now, you don't need any other RegexOption. In your pattern you have to choose SingleLine to also match "\n" with the ".".

    If there is a "{", and after it any character but "}", it's a match.

    To also identify the last ";", it's handled extra. If something is on the "sem"-stack, only match the ";", otherwise add it to the "sem"-stack. With this, only the first occurence of the semicolon is on the stack. Due to (?<=), the characters are matched from right to left, therefore you will need the first occurence to match the last semicolon.

    After all, you have a group called "sem", that matches the last semicolon. In your replace part, you have to add there your closing "}" - maybe there's an easier solution, but it's the first that comes into my mind ;)

    Can't work with nested constructs.

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de

    • Marked As Answer by Sovan Kumar Das Tuesday, February 21, 2012 6:12 AM
    •  
  • Monday, February 20, 2012 3:39 PM
     
     

    RegexOptions.MultiLine only affects ^ and $ in a pattern (changes teh meaning to start/end of line instead start/end of text).

    RegexOption.SingleLine affects "." so that "." matches really every character. Without this option, "." it's the very same as "[^\n]"

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de

  • Tuesday, February 21, 2012 6:14 AM
     
     

    Hello,

    Thanks a trillion. Good solution.

    sovan kumar das