Solved

How to code repeated regular expresion in VB.net for xml files

Posted on 2011-02-21
3
276 Views
Last Modified: 2013-11-26
Hello I am looking for a solution to simple VB.net coding issue.
The problem is easy to explain but difficult to code for novice like us.

We are making translation program in VB.net fot xml reporting files. Our
reporting database already include translation expression for every word which should be translated in our reports, which looks like: code.translate("WordToBeTranslated")
We have already decoded it into phase which get us back all WordToBeTranslated list from whole report. But only if textbox contain exactly one code.translate expression. We are using nodes and node list with regular expression.

This regular expresions and matching in VB code looks exactly like this:

New Regex("^=code.Translate\(\""(.*)\"")$"), RegexOptions.IgnoreCase)

m= pattern.Match(label)

So, if theres more than one code.translate, program gets back everything from first "quote" to the latest quote and is unable to detect each expression separately.

Please if you can give me any advice how to resolve this, I can really appreciate it ;)
Thanks,
Vojtech
0
Comment
Question by:psionic086
  • 2
3 Comments
 
LVL 8

Expert Comment

by:crysallus
ID: 34941808
This should probably help you understand what's going on, and give you a method to fix it (better than me simply giving you the solution as this way you're more likely to understand the solution).

http://www.regular-expressions.info/repeat.html
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 34943050
This is because you are using a greedy star. This will try to capture as much as possible that makes a valid match. What you want is the non-greedy version of the star--which you can achieve by adding a question mark after the star:

    New Regex("^=code.Translate\(\""(.*)\"")$"), RegexOptions.IgnoreCase)

However, I'd suggest rather than using dot-star, use "not quotation mark" to search:

    ew Regex("^=code.Translate\(\""([^"]*)\"")$"), RegexOptions.IgnoreCase)

Then, as long as a quote follows the string to find, you won't have to worry about greedy vs. non-greedy.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34943054
Forgot the question mark for Example 1:

    New Regex("^=code.Translate\(\""(.*?)\"")$"), RegexOptions.IgnoreCase)
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question