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

PCRE Regex Question Re: Character Count

Last post 05-31-2009, 10:57 PM by mash. 1 replies.
Sort Posts: Previous Next
  •  05-31-2009, 6:35 PM 53479

    PCRE Regex Question Re: Character Count

    So basically I use a program that lets you filter incoming text using regexs. It runs them off the PCRE library.

    The incoming text I'm looking to filter comes in the form of

    #K%<5 digit ID><3 digit character count><3 alpha literal><data>

    i.e.

    #K%00675011AAF3.0 days

     

    The 5 digit ID is known ahead of time, the 3 digit character count is the length of the data + 3 for the alpha literal. The problem is that these sequences may or may not have a newline in front of them, and may or may not come in bunches with other text or other #K sequences, so what I'm trying to do is write a regex that will, by grabbing that 3 digit character count 'x', grab only the following 'x' characters after it. This appears to be way above my level of expertise as I'm guessing it needs to do some sort of look ahead?

    Right now I'm grabbing as much as the regex will grab with (?:^)?\#K%(?:@MIP_ID)($count:\d{3})($literal:\a{3})($data:.*) and then using a right() style function to chop the data bit to a length of $count-3, then re-displaying the leftover chunk so the regex can grab it again. Is there a way to do something like (?:^)?\#K%(?:@MIP_ID)($count:\d{3})($literal:\a{3})($data:.{$count}) because that doesn't seem to compile...

     

    Any help would be greatly appreciated.

  •  05-31-2009, 10:57 PM 53488 in reply to 53479

    Re: PCRE Regex Question Re: Character Count

    You can't do what you are trying to do with just a regex. Regexes are declaritive, once define you can not redifine them during execution. You'd need code and multiple regexes do  what you want. If you gave some more details about the program you are using and a more complete sample of your source someone may be able to help.

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
View as RSS news feed in XML