Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Match first word after white space skip all characters until last opening square...

Last post 03-12-2008, 3:47 PM by wleeb. 5 replies.
Sort Posts: Previous Next
  •  03-11-2008, 4:09 PM 40236

    Match first word after white space skip all characters until last opening square...

    Goal: 

    Match the first word (named group <type>) after any number of white spaces then match the last occurence of space+colon+equal+space+opening square ie ( := [) and return the opening square along with all characters upto and including a last closing square + semi-colon + carriage return (named group <value>). The named group <type> must ignore all characters between after the first matched word and the start of named group <value>.

    Problem:

    The regular expression below matches if there are no characters are between <type> and <value> other than space+colon+equal+space ie ( := ), however if characters do exist then it does not match...ugh!

    Current Match:

    <type> : InputForceData

    <value> : [0,2,0,6,1,0,0,0,3]

    Desired Match:

    <type> : InputData

    <value> : [0,1,2,3,4]

    <type> : InputForceData

    <value> : [0,2,0,6,1,0,0,0,3]

    Platform:

    .Net (VS2008)

    Regular Expression

    \s*(?<type>\b\w+\b*?)\s:=\s(?<value>\[.*?);\r

    Sample Text:

       CONNECTION StandardInput (Rate := 10000,
                                 EventID := 0)
         InputData (COMMENT.DATA.0 := "P903_RUN_STAT",
    COMMENT.DATA.1 := "P905_RUN_STAT") := [0,1,2,3,4];
         InputForceData := [0,2,0,6,1,0,0,0,3];
       END_CONNECTION

     

  •  03-11-2008, 4:56 PM 40237 in reply to 40236

    Re: Match first word after white space skip all characters until last opening square...

    This worked for me:

              Regex re = new Regex(@"(?<type>\b\w+\b.*?)\s*(?:\([^)]*\))?\s*:=.*?(?<value>\[[^\]]*\]);");
              MatchCollection mc = re.Matches(sourcestring);


  •  03-11-2008, 5:43 PM 40241 in reply to 40237

    Re: Match first word after white space skip all characters until last opening square...

    Thanks!!!
  •  03-11-2008, 6:59 PM 40243 in reply to 40237

    Re: Match first word after white space skip all characters until last opening square...

    Worked great for the sample text however when I test real world text it truncates at the first closing square whenvever <value> contains nested squares.

    Sample Text:

     CONNECTION StandardInput (Rate := 10000,
                                 EventID := 0)
         InputData (COMMENT.DATA.0 := "P903_RUN_STAT",
    COMMENT.DATA.1 := "P905_RUN_STAT") := [0,[1],2,3,4];
         InputForceData := [0,2,0,6,1,0,0,0,3];
       END_CONNECTION

    Current Match:

    <type> : InputData

    <value> : [0,[1]

    <type> : InputForceData

    <value> : [0,2,0,6,1,0,0,0,3]

    Desired Match:

    <type> : InputData

    <value> : [0,[1],2,3,4]

    <type> : InputForceData

    <value> : [0,2,0,6,1,0,0,0,3]

  •  03-11-2008, 8:31 PM 40250 in reply to 40243

    Re: Match first word after white space skip all characters until last opening square...

              Regex re = new Regex(@"(?<type>\b\w+\b.*?)\s*(?:\([^)]*\))?\s*:=.*?(?<value>\[.*?\]);");

  •  03-12-2008, 3:47 PM 40260 in reply to 40250

    Re: Match first word after white space skip all characters until last opening square...

    It worked, thanks!
View as RSS news feed in XML