0

In C# I want to search for where a Match occurs starting with an integer in parenthesis followed by these characters "PLA" parenthesis and "match" this (I'll read into memory) until the next set is reached.

Thus the sample code would be

(1965
    ("PLA")
    ("GEN_ANGLE")
    ("Line to line angle")
    (
        ("clinesegs" 3565.01 1265.99 "SURFACE")
        ("clinesegs" 3618.02 1255.00 "SURFACE")
    )
    ((3586.02 1267.20 "SURFACE"))
    (120.000)
    (90.000)
)
(1966
    ("PLA")
    ("GEN_ANGLE")
    ("Line to line angle")
    (
        ("clinesegs" 3831.98 1255.00 "SURFACE")
        ("clinesegs" 3882.92 1268.07 "SURFACE")
    )
    ((3863.98 1267.20 "SURFACE"))
    (120.000)
    (90.000)
)

I WANT to "match" the data and only grab this data based on knowing that "1965" is the ID i'm looking for.

(1965
    ("PLA")
    ("GEN_ANGLE")
    ("Line to line angle")
    (
        ("clinesegs" 3565.01 1265.99 "SURFACE")
        ("clinesegs" 3618.02 1255.00 "SURFACE")
    )
    ((3586.02 1267.20 "SURFACE"))
    (120.000)
    (90.000)
)

I can find "(1965" with:

(\(1965)  

.. or (with (ADD) in front):

[(](ADD){1}\r\n\r\n\t\s[(][0-9]{4,}\r\n\r\n\t\s\s(\("){1}[a-zA-Z]{1,}("\)){1}

.. but I can't seem to really get these types of regex to work it must be spacing and line breaks
I am stuck with understanding the matching of end PLA and "detecting" the ending ) before the next set of data starts (1966 ("PLA") as I figured that is what I would use in the match to detect the end of the match, but just not include it in the findings.

David
  • 15,894
  • 22
  • 55
  • 66
  • 1
    Please make sure to read through answers for top rated [regex-match-open-tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?rq=1) before you start trying to match brace pairs (possible, but other methods may be easier). – Alexei Levenkov Apr 20 '13 at 05:24

3 Answers3

0

If you know that it's followed by (1966 ("PLA") which doesn't occur inside of the data, you can use something like:

(?xs)                     # ignore spaces, comments and make . match \n
\(1965\s+\("PLA"\)
.*?                       # ungreedy
(?=\(1966\s+\("PLA"\)|$)  # lookahead does not include this in the match

Which can be quoted in C# like so:

var re = @"(?xs)               # ignore spaces, comments and make . match \n
    \(1965\s+\(""PLA""\)
    .*?                        # ungreedy
    (?=\(1966\s+\(""PLA""\)|$) # lookahead does not include this in the match
    ";
Qtax
  • 33,241
  • 9
  • 83
  • 121
0

This is what I came up with:

EDIT: I modified the expression to account for N number of "clinesegs", works with any number of groups before and after the "Line to line angle" group.

\([\d]+[\s]+\("PLA"\)([\s]+\(.+)+[\s]+\(([\s]+\(.+)+[\s]+\)([\s]+\(.+\))+[\s]+\)

It will capture everything from (1965 until the closing ).

bitwalker
  • 9,061
  • 1
  • 35
  • 27
  • but you are matching 11 times (11 lines ) what happens when I have 40 of those "Line to line angle" , the "clinesegs" ? then just merely counting is not going to end the match successful, I need to find where the next "record" starts which is "(1966 ("PLA")" Yes I wish it was done in xml, but this is what I have to work with. thx –  Apr 20 '13 at 08:34
  • Ok, I went back to the drawing board and figured this out for you. This expression is a bit long, but it accounts for N "clinesegs". Right now it expects the rest of the items to be the same (more or less), but if you look at the pattern, you should be able to mold it to be more flexible if you need it to be. The edits are in my original post. – bitwalker Apr 20 '13 at 18:45
0

try this pattern: use your id at 1965 in this pattern

(?=\(1965)(?=.*\(\"PLA\"\).*)\(((([^)])*\([^)]+\)[^)]*)\))*\)*[^\)]*\)

use RegexOptions.SingleLine

Civa
  • 2,058
  • 2
  • 18
  • 30
  • I pasted it into "RegexBuddy" as I just bought that tool today, but maybe I'm not doing something right, i will look again –  Apr 20 '13 at 08:28