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

Reg Expression

Last post 03-27-2008, 10:54 PM by mash. 1 replies.
Sort Posts: Previous Next
  •  03-27-2008, 10:32 PM 40764

    Reg Expression

    Hi,

    I am in need for Regular Expression pattern that could help me parse what I would call tokens. Currently, I am parsing a string character by character and using a scanner approach (Sort of how compiler works). However, I would think a RegExp would perform better. 

    Here is the scenario: Everything that is used within a “()” is called a token. A token could have nested tokens. An example would be:

    Say, I have a token called (CurrentUser)and I have another token called (Date). Each token could support embedded attributes inside it. For example, the Date might have format=”Long|short|longtime|shortime”.

    So if I have a string that is “(CurrentUser) logged on (Date: format=”short”). I should be able to replace that string by “John Smith logged on 03/30/3008” That was simple, however it could get more complex. For example say “(CurrentUser)  logged on (Date: format=”(defaultFormat)” ) . the way my algorithm works is first I find inner tokens

    (CurrentUser) : John Smith

    (defaultFormat) : Long

    Step 1  “(CurrentUser)  logged on (Date: format=”(defaultFormat)” )

    Step2  “(CurrentUser)  logged on (Date: format=”long” )

    At this point, I start processing form start again and I will do string replacement on the tokens.

    I am hoping to find a creative RegExp pattern that I could use to parse it in a better way.

    Thanks,

    Filed under:
  •  03-27-2008, 10:54 PM 40766 in reply to 40764

    Re: Reg Expression

    This is not something a regex is well suited for. Regexes are for pattern matching, parsing is more complicated than that. Regexes are a poor substitute for a parser. You didn't mention which language you were doing this in but only a few regex engines support nested constructs. Even those won't return the nested value as a separate match  which is what I'm guessing you want.

    Michael

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