Solved

C#: Regex TRIM on the fly?

Posted on 2004-09-15
5
478 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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