How do I write a regex that matches all characters that are not a '$' followed by 'i' or '{'?
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 every开发者_StackOverflowthing 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)|\$(?!\{)
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.
精彩评论