473,326 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Regex - Is this the best expression to match with...

This works but I was wondering if there is a better way to write this
expression?

Input text: multi-word | optional space = optional space | single word | ;
optional carriage return
---------------------------------
sometext=something;
moretext = something;
other text = something;

Expression:
---------------------------------
(?<key>(\w+\s*)+)\s*=\s*(?<data>(\w+\s*)+);[\r\n]*

Nov 17 '05 #1
1 1163
In article <F5**********************************@microsoft.co m>,
Dave <Da**@discussions.microsoft.com> wrote:

: This works but I was wondering if there is a better way to write this
: expression?
:
: Input text: multi-word | optional space = optional space | single
: word | ; optional carriage return
: ---------------------------------
: sometext=something;
: moretext = something;
: other text = something;
:
: Expression:
: ---------------------------------
: (?<key>(\w+\s*)+)\s*=\s*(?<data>(\w+\s*)+);[\r\n]*

I'd write it this way:

(?<key>[\w\s]+?)\s*=\s*(?<data>\S+);\s*

The non-greedy + in key serves to right-trim the matched substring.
As a rule of thumb, wrapping layers of quantifiers gives the matcher
*lots* of possibilities to try, so avoid writing such patterns.

In your spec above, you said data is a single word, but you used
the same pattern as for key. Using \S+ matches a run of non-whitespace
characters.

I used \s* at the end rather than [\r\n]* because this looks like a
human-edited config file, and in that context, it's good to be
forgiving about invisible characters. If that reasoning does apply,
consider using ;+ (i.e., one or more semicolons) too.

Hope this helps,
Greg
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ali Eghtebas | last post by:
Hi, I've made this regex to catch the start of a valid multiline comment such as "/*" in e.g. T-SQL code. "(?<=^(?:*'*')*?*)(?<!^(?:*'*')*?--.*)/\*.*?$" With Multiline option on. As we know...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
2
by: Daniel Billingsley | last post by:
First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking in this area... it's just a whole bunch of information thrown at you and you're left to yourself as to organizing it in...
2
by: John Baro | last post by:
I need to determine when multiple fonts are selected in a richtextbox. A font is indicated by \fcharset(N) where (N) is a number. (To the best of my knowledge) I can use this statement int...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
4
by: Sarah | last post by:
Hi - I am extremely new to programming so I apologize in advance for my lack of knowlege, vocabulary and if my code is sloppy. I will take any suggestions to clean up my code and to follow any...
7
by: =?Utf-8?B?amFj?= | last post by:
Hi, I have problems with following code and don’t find the bug : // Set ArrayList aArray = new ArrayList(); regStr = new Regex(@"\?)*(\d+)\]"); if(text != null && regStr.IsMatch(text))...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.