Solved

C#: Regex TRIM on the fly?

Posted on 2004-09-15
5
474 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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