Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyparsing - name not starting with a character

Tags:

pyparsing

I am trying to use Pyparsing to identify a keyword which is not beginning with $ So for the following input:

$abc = 5 # is not a valid one
abc123 = 10 # is valid one
abc$ = 23 # is a valid one

I tried the following

var = Word(printables, excludeChars='$')
var.parseString('$abc') 

But this doesn't allow any $ in var. How can I specify all printable characters other than $ in the first character position? Any help will be appreciated.

Thanks Abhijit

like image 679
user3138594 Avatar asked Jan 25 '26 01:01

user3138594


1 Answers

You can use the method I used to define "all characters except X" before I added the excludeChars parameter to the Word class:

NOT_DOLLAR_SIGN = ''.join(c for c in printables if c != '$')
keyword_not_starting_with_dollar = Word(NOT_DOLLAR_SIGN, printables)

This should be a bit more efficient than building up with a Combine and a NotAny. But this will match almost anything, integers, words, valid identifiers, invalid identifiers, so I'm skeptical of the value of this kind of expression in your parser.

like image 107
PaulMcG Avatar answered Jan 27 '26 01:01

PaulMcG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!