Solved

C#: Regex TRIM on the fly?

Posted on 2004-09-15
5
467 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
Comment Utility
Regex.Replace(strLog, @"\((\d{1,6})\)", "$1");
0
 
LVL 1

Author Comment

by:kvnsdr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now