views:

83

answers:

1

Meaning, I want to match:

$10

or

$

but not this:

${name}

or:

$image{http://wrfgadgadga.com/gadgad.png}

I also want to match everything else... normal characters, symbols, numbers, etc.

Matching everything but things that start with $ is easy. It's like this:

def literalCharacter: Parser[String] = """[^\$]""".r

I've tried the regular expression look-ahead syntax using (?!i) or (?!{) in numerous combinations but I can't seem to get it to work. I've also tried rewriting it with the = instead of the ! like this: (?=i)

Basically, I've tried injecting these look-aheads in every way I can image with the [^\$] expression and I can't get it work.

Help?

EDIT: Hrm, this seems to work:

[^\$]|\$(?!i)|\$(?!\{)
+3  A: 

Yours won't match strings like x$ properly. If you want to match the entire string, then try

"""^\$$|^[^\$].*$|^\$[^i\{].*$"""

where we are matching either of three sequences separated by |:

^\$$
^[^\$]+.*$
^\$[^i\{]+.*$

Let's take this apart:

// First pattern--match lone '$' character
^   // Matches start of string
\$  // Matches the literal character '$'
$   // Matches end of string

// Second pattern--match a string starting with a character other than '$'
^       // Start of string
[^\$]+  // Match at least one non-'$':    
           +   // Match one or more
      [^  ]    // ...of characters NOT listed...
        \$     // ...namely, a literal '$'
.*      // Any number of other characters
$       // End of the string

// Third pattern--match a string starting with '$' but not '$i' or '${'
^        // Start of string
\$       // The literal character '$'
[^i\{]+  // Match at least one non-'i'/non-'{'
.*       // Any number of other characters
$        // End of the string

If you don't match the whole string, you have to worry about things like foo$image{Hi}. If you want to match the empty string also, prepend ^$| to the match.

Note that this is written to work specifically with regexes, not with your parser combinator in mind. Depending on what other rules you have, you may or may not want to match the whole string.

Rex Kerr
Wow, thanks for that insight into why my regex isn't perfect. Yes, I don't want to match the whole string. I have a bunch of text to parse in the grammar, and so I really only want to read a sub-set of chunks at a time. Basically, I want to stop whenever it gets something meaningful, like $image{...} or ${...}. If it's not meaningful, I just wanted the parser to return itself, otherwise, do something special. I'll try your stuff out in a bit and let you know how it goes ;) Thanks for all that hard work.
egervari