0

Possible Duplicate:
RegEx match open tags except XHTML self-contained tags

I have a text file consist of conversion instruction templates. I need to parse this text file,

I need to match something like this:

(Source: <element>)

And get the "element".

Or this pattern:

(Source: <element attr="name" value=""/>)

And get "element attr="name"".

I am currently using this regex:

\(Source:\ ?\<(.*?)\>\)

Sorry for being a newbie. :)

Thanks for all your help.

-JRC
Community
  • 1
  • 1
jomsk1e
  • 3,585
  • 7
  • 34
  • 59

1 Answers1

2

Try this Regex for detect attibs by both or " characters:

\(Source:\s+<(\w+\s+(?:\w+=[\"”][^\"”]+[\"”])?)[^>]*>\)

and your code:

var result = Regex.Match(strInput, 
                         "\\(Source:\\s+<(\\w+\\s+(?:\\w+=[\"”][^\"”]+[\"”])?)[^>]*>")
                  .Groups[1].Value;

explain:

(subexpression) Captures the matched subexpression and assigns it a zero-based ordinal number.

? Matches the previous element zero or one time.

\w Matches any word character.

+ Matches the previous element one or more times.

Ria
  • 10,237
  • 3
  • 33
  • 60
  • this is good, but it only gets the element name, if the element has an attribute I want to extract the 1st attribute also. – jomsk1e Nov 08 '12 at 06:16
  • I updated my answer. just get group indexed 1 – Ria Nov 08 '12 at 06:21
  • it only extract the element name :(( I am using Expresso to try the regex expression huhu – jomsk1e Nov 08 '12 at 06:33
  • I tested my code in a .NET app! and I'm using [radsoftware RegexDesigner](http://www.radsoftware.com.au/?from=RegexDesigner) – Ria Nov 08 '12 at 06:36
  • for example you have a string = (Source: ).... I want to get this--> Property FormalName="Country", your regex only extract "Property" – jomsk1e Nov 08 '12 at 07:02
  • Your sample contains `”` character and my regex detect attibs by `"` character. – Ria Nov 08 '12 at 07:12
  • Oh.. Thanks for that! Sorry for being a newbie! :) – jomsk1e Nov 08 '12 at 07:15