CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] Regex not working?

    I am trying to return the name of a particular company from a textfile. The company name is always displayed after the German word "Gewerbeamt" and before the html symbols. On the following extract you can see for example that the first name is "Aldingen", the second "Alfdorf" and so forth:

    Code:
    <p class="liste"><a href="gewerbeamt_11730.php">Gewerbeamt Aldingen</a><br>
    78549 Spaichingen</p>
    <hr align="left" class="liste"><p class="liste"><a href="gewerbeamt_11640.php">Gewerbeamt Alfdorf</a><br>
    73553 Alfdorf</p>
    <hr align="left" class="liste"><p class="liste"><a href="gewerbeamt_4474.php">Gewerbeamt Allensbach</a><br>
    78462 Konstanz</p>
    <hr align="left" class="liste"><p class="liste"><a href="gewerbeamt_9127.php">Gewerbeamt Alleshausen</a><br>
    88422 Bad Buchau</p>
    I have used Regex to write a pattern for this which I succesfully tested using the Online Regex testing tool (http://www.gskinner.com/RegExr/). When I try to implement this pattern it however returns null!

    Code:
    protected void Regex(string value)
            {
                    string lsPattern = "/(?<=Gewerbeamt ).*(?=</a>)/g"; // @?
    
                    // Compile the regular expression.
                    Regex r = new Regex(lsPattern, RegexOptions.IgnoreCase);
    
                    // Match the regular expression pattern against a text string.
                    Match m = r.Match(value);
                   
                    string lsContent = m; // Returns null! Is this correct way of implementing?
                }

  2. #2
    Join Date
    Jan 2010
    Posts
    130

    Re: Regex not working?

    I solved it! The solution is as follows:

    Code:
    protected void SplitLineRegex(string value)
            {
                try
                {
                    string lsPattern = "(?<=Gewerbeamt ).*(?=</a>)"; // "/(?<=Gewerbeamt ).*(?=</a>)/g";
    
                    // Compile the regular expression.
                    Regex r = new Regex(lsPattern, RegexOptions.IgnoreCase);
    
                    // Match the regular expression pattern against a text string.
                    Match m = r.Match(value);
    
                    // Store values in string name
                    msName += m.Value + "\r\n";
    
                    tbxDisplay.Text = msName;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured