Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

RegEx Help Needed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
billy.murray@scomagg.com - 29 Jun 2007 15:12 GMT
I have the following text inside a file which I am trying to parse.

I am creating a new RegEx object as follows :-
string strRegex = @"({S:\r?\n)([^}]*)(})"

{S: INTERLOCK FAIL     : Tie bar hoist - rotate anticlockwise
 I1-T/bar hoist slew handwind eng, input LS/TBHS/HWEa is
LOW
 I1-T/bar hoist slew handwind eng, input LS/TBHS/HWEb is
HIGH
}

Calling the match method of strRegex provides no matches, however if I
move 'INTERLOCK FAIL' onto a new line and ensure there is no space
after the '{S:' the match method returns the text I am looking for.
For the life of me I cannot fathom out the expression required to
capture text regardless of whether it is on the same line as the {S:
or not and also if there is white space after the {S:.

I could manually restructure the text if it was only a few changes,
trouble is I have over 1000 files each containing possibly 100s of
these text messages.

Can anoyone out there shed some light on the problem.

Thanks
Jesse Houwing - 29 Jun 2007 15:21 GMT
* billy.murray@scomagg.com wrote, On 29-6-2007 16:12:
> I have the following text inside a file which I am trying to parse.
>
[quoted text clipped - 20 lines]
>
> Can anoyone out there shed some light on the problem.

> string strRegex = @"({S:\r?\n)([^}]*)(})"

Your current regex reads as follows:

find the following text: {S:
optionally find a carrige return (\r?)
find a newline (\n)
find any text that is not a } [^}]*
find the closing bracket }

It also contains a lot of ( and ) that I think only make it less readable.

I'd try this alternative:

{S:(?<content>[^}]*)}

Or if you don't want any leading spaces/tabs/newlines:

{S:\s*(?<content>[^}]*)}

which reads as follows:

find {S:
optionally find any number  whitespace characters (\s*)
find and capture any text that is not a } ([^}]*) in a named group
called "content"
find a closing bracket (})

To get the value from the parsed text you can then use the following code:

Regex rx = new Regex(@"{S:\s*([^}]*)}");
Match m = rx.Match(".... text to match .....");
string match = m.Value;
string content = m.Groups["content"].Value;

Jesse Houwing

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.