Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:55:22+00:00 2026-05-23T02:55:22+00:00

I’m building a syntax parser to perform simple actions on objects identified using dotted

  • 0

I’m building a syntax parser to perform simple actions on objects identified using dotted notation, something like this:

DISABLE ALL;
ENABLE A.1 B.1.1 C

but in DISABLE ALL the keyword ALL is instead matched as 3 Regex(r'[a-zA-Z]') => 'A', 'L', 'L' I use to match arguments.

How can I make a Word using regex? AFAIK I can’t get A.1.1 using Word

please see example below

import pyparsing as pp

def toggle_item_action(s, loc, tokens):
    'enable / disable a sequence of items'
    action = True if tokens[0].lower() == "enable" else False
    for token in tokens[1:]:
        print "it[%s].active = %s" % (token, action)

def toggle_all_items_action(s, loc, tokens):
    'enable / disable ALL items'
    action = True if tokens[0].lower() == "enable" else False
    print "it.enable_all(%s)" % action

expr_separator = pp.Suppress(';')

#match A
area = pp.Regex(r'[a-zA-Z]')
#match A.1
category = pp.Regex(r'[a-zA-Z]\.\d{1,2}')
#match A.1.1
criteria = pp.Regex(r'[a-zA-Z]\.\d{1,2}\.\d{1,2}')
#match any of the above
item = area ^ category ^ criteria
#keyword to perform action on ALL items
all_ = pp.CaselessLiteral("all")

#actions
enable = pp.CaselessKeyword('enable')
disable = pp.CaselessKeyword('disable')
toggle = enable | disable

#toggle item expression
toggle_item = (toggle + item + pp.ZeroOrMore(item)
    ).setParseAction(toggle_item_action)

#toggle ALL items expression
toggle_all_items = (toggle + all_).setParseAction(toggle_all_items_action)

#swapping order to `toggle_all_items ^ toggle_item` works
#but seems to weak to me and error prone for future maintenance
expr = toggle_item ^ toggle_all_items
#expr = toggle_all_items ^ toggle_item

more = expr + pp.ZeroOrMore(expr_separator + expr)

more.parseString("""
    ENABLE A.1 B.1.1;
    DISABLE ALL
    """, parseAll=True)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:55:23+00:00Added an answer on May 23, 2026 at 2:55 am

    Is this the problem?

    #match any of the above
    item = area ^ category ^ criteria
    #keyword to perform action on ALL items
    all_ = pp.CaselessLiteral("all")
    

    Should be:

    #keyword to perform action on ALL items
    all_ = pp.CaselessLiteral("all")
    #match any of the above
    item = area ^ category ^ criteria ^ all_
    

    EDIT – if you’re interested…

    Your regexes are so similar, I thought I’d see what it would look like to combine them into one. Here is a snippet to parse out your three dotted notations using a single Regex, and then using a parse action to figure out which type you got:

    import pyparsing as pp
    
    dotted_notation = pp.Regex(r'[a-zA-Z](\.\d{1,2}(\.\d{1,2})?)?') 
    def name_notation_type(tokens):
        name = {
            0 : "area",
            1 : "category",
            2 : "criteria"}[tokens[0].count('.')]
        # assign results name to results - 
        tokens[name] = tokens[0] 
    dotted_notation.setParseAction(name_notation_type)
    
    # test each individually
    tests = "A A.1 A.2.2".split()
    for t in tests:
        print t
        val = dotted_notation.parseString(t)
        print val.dump()
        print val[0], 'is a', val.getName()
        print
    
    # test all at once
    tests = "A A.1 A.2.2"
    val = pp.OneOrMore(dotted_notation).parseString(tests)
    print val.dump()
    

    Prints:

    A
    ['A']
    - area: A
    A is a area
    
    A.1
    ['A.1']
    - category: A.1
    A.1 is a category
    
    A.2.2
    ['A.2.2']
    - criteria: A.2.2
    A.2.2 is a criteria
    
    ['A', 'A.1', 'A.2.2']
    - area: A
    - category: A.1
    - criteria: A.2.2
    

    EDIT2 – I see the original problem…

    What is messing you up is pyparsing’s implicit whitespace skipping. Pyparsing will skip over whitespace between defined tokens, but the converse is not true – pyparsing does not require whitespace between separate parser expressions. So in your all_-less version, “ALL” looks like 3 areas, “A”, “L”, and “L”. This is true not just of Regex, but just about any pyparsing class. See if the pyparsing WordEnd class might be useful in enforcing this.

    EDIT3 – Then maybe something like this…

    toggle_item = (toggle + pp.OneOrMore(item)).setParseAction(toggle_item_action)
    toggle_all = (toggle + all_).setParseAction(toggle_all_action)
    
    toggle_directive = toggle_all | toggle_item
    

    The way your commands are formatted, you have to make the parser first see if ALL is being toggled before looking for individual areas, etc. If you need to support something that might read “ENABLE A.1 ALL”, then use a negative lookahead for item: item = ~all_ + (area ^ etc...).
    (Note also that I replaced item + pp.ZeroOrMore(item) with just pp.OneOrMore(item).)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.