RegEx Help Needed

  • Thread starter Thread starter billy.murray
  • Start date Start date
B

billy.murray

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
 
* (e-mail address removed) wrote, On 29-6-2007 16:12:
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.
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
 
Back
Top