473,379 Members | 1,201 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,379 software developers and data experts.

Regular Expression Capture groups

Plater
7,872 Expert 4TB
I'm having no end of trouble getting a correct regular expression for what I am doing. It probably would have taken me 5mins to produce a plain text matching system with string indexes, but even after an hour of failing, I still want to use regex.

I have something like the following as input text
Expand|Select|Wrap|Line Numbers
  1. #define  HIT_VAL1       4               //  some comment
  2. #define  HIT_VAL2       5               //  some comment
  3. #define  HIT_VAL3       6               //  some comment
  4.  
And I am looking to pull 2 pieces of information from each line.
So for the following line, I want the bold parts
#define HIT_VAL1 4 // some comment
_______________^ not bold


I have tried various variations on this for just the first bold section:
Regex rx = new Regex(@"^#define HIT_(\w)*", RegexOptions.IgnoreCase);
or
Regex rx = new Regex(@"^(?:#define HIT_)(\w)*", RegexOptions.IgnoreCase);

But all I ever get is:
#define HIT_VAL1
#define HIT_VAL2
#define HIT_VAL3

(?: ) said it should be a NON capture group, I want to ignore that part. I could just pop it off with a string.replace() but I still feel regular expressions should be useful for SOMETHING.
Does anyone have any thoughts? I keep going through regex tutorials, but apparently regex was designed to only do a single match? That seems very poorly thought out?
Jul 10 '09 #1
1 2104
Plater
7,872 Expert 4TB
I think I figured it out. Was a problem with how I looked at the matches vs groups.
Assuming my input string is WholeData, the following worked:
Expand|Select|Wrap|Line Numbers
  1. Regex rx = new Regex(@"^#define[\s|\t]+(?<preType>\w*)_(?<name>\w*)[\s|\t]+(?<value>\w*)[\s|\t]+//[\s|\t]*(?<comment>.*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
  2. MatchCollection matches = rx.Matches(WholeData);
  3. Console.WriteLine("MatchesCount "+matches.Count);
  4. foreach (Match m in matches)
  5. {
  6.    //Console.WriteLine("Match: '" + m.Value + "'");
  7.    //Console.WriteLine("GroupCount: " + m.Groups.Count);
  8.    Console.WriteLine("Want name: " + m.Groups["name"].Value);
  9.    Console.WriteLine("Want value: " + m.Groups["value"].Value);
  10.    Console.WriteLine("Want comment: " + m.Groups["comment"].Value);
  11. }
  12.  
Jul 10 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
5
by: Trevor Braun | last post by:
Hi, I'm not sure that this is the right forum for this, but I've been having a very tough time completing this expression, and I was hoping someone might have some suggestions for me. I am trying...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
5
by: Cylix | last post by:
I am going to write a function that the search engine done. in search engine, we may using double quotation to specify a pharse like "I love you", How can I using regular expression to sperate...
3
by: Zach | last post by:
Say I have a string which is of the format {A, B, C, D} for some variable number of objects. I want a regular expression that will put each of A, B, C, and D into its own separate capture...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: Zeba | last post by:
Hi guys, I need some help regarding regular expressions. Consider the following statement : System.Text.RegularExpressions.Match match =...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
4
by: JJ | last post by:
I can get a set of matches of URL's by using a match expression that has a named capture group in it, i.e.: Regex reg_linkTags = new Regex("(?:.....long resular...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.