?
Solved

C#: Regex TRIM on the fly?

Posted on 2004-09-15
5
Medium Priority
?
484 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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