Solved

C#: Regex TRIM on the fly?

Posted on 2004-09-15
5
472 Views
Last Modified: 2010-05-18
I'm reading 'live' server log information (before written to log.txt) into an MS-SQL database. All is well and performance is outstanding. I would like to improve my Regex parsing to clean-up my SQL data for easier processing. For instance,

This Regex will find any 'Port' number from 1 - 999999 contained within ( and ).......
(\((\d{1,6})\))      = (1) to (999999) Port Addresses

Although, I would like to 'Trim' off the ( and ) before it is written to the database so the port looks like 25 and not (25).

Q. How can I painlessly perform the 'Trim' task in Regex
0
Comment
Question by:kvnsdr
5 Comments
 
LVL 1

Expert Comment

by:CrazyIvan007
ID: 12069857
Regex.Replace(strLog, @"\((\d{1,6})\)", "$1");
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12070053
I'm not quite sure how to cleanly implement your code with my existing code as follows:

string pattern4 = @"(\((\d{1,6})\))";
Regex r4 = new Regex(pattern4, RegexOptions.Compiled);
Match Port = r4.Match(str);
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 250 total points
ID: 12070151
I think what you are looking for is:

  string pattern4 = @"((?<=\()(\d{1,6})(?=\)))";
  Regex r4 = new Regex(pattern4, RegexOptions.Compiled);
  Match Port = r4.Match("asdasads (123123) asdasd");
0
 
LVL 19

Expert Comment

by:drichards
ID: 12070227
Actually, your regex has already done that for you - you have groups defined in your regex expression.
Try:

   string port = r4.Match.Groups[r4.Groups.Count-1]; // add .Value to the end for completeness ...
   string port = r4.Match.Groups[r4.Groups.Count-1].Value;

Also, your regex is overkill, you don't need the forst parens.  This just makes the entire match another group, which is redundant.  You will get the same result with:

    string pattern4 = @"\((\d{1,6})\)";

But without a redundant group.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12070269
Oh yeah, as drichards said you don't need the outside brackets... so:

  string pattern4 = @"(?<=\()\d{1,6}(?=\))";

Would work just fine.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 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