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 8398731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:07:25+00:00 2026-06-09T21:07:25+00:00

This an optimized version of the tokenizer that was first written, and it works

  • 0

This an optimized version of the tokenizer that was first written, and it works fairly well. A secondary tokenizer can parse the output from this function to create classified tokens of greater specificity.

def tokenize(source):
    return (token for token in (token.strip() for line
            in source.replace('\r\n', '\n').replace('\r', '\n').split('\n')
            for token in line.split('#', 1)[0].split(';')) if token)

My question is this: how can this be written simply with the re module? Below is my ineffective attempt.

def tokenize2(string):
    search = re.compile(r'^(.+?)(?:;(.+?))*?(?:#.+)?$', re.MULTILINE)
    for match in search.finditer(string):
        for item in match.groups():
            yield item

Edit: This is the type of output that I am looking for from the tokenizer. Parsing the text should be easy.

>>> def tokenize(source):
    return (token for token in (token.strip() for line
            in source.replace('\r\n', '\n').replace('\r', '\n').split('\n')
            for token in line.split('#', 1)[0].split(';')) if token)

>>> for token in tokenize('''\
a = 1 + 2; b = a - 3 # create zero in b
c = b * 4; d = 5 / c # trigger div error

e = (6 + 7) * 8
# try a boolean operation
f = 0 and 1 or 2
a; b; c; e; f'''):
    print(repr(token))


'a = 1 + 2'
'b = a - 3 '
'c = b * 4'
'd = 5 / c '
'e = (6 + 7) * 8'
'f = 0 and 1 or 2'
'a'
'b'
'c'
'e'
'f'
>>> 
  • 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-06-09T21:07:27+00:00Added an answer on June 9, 2026 at 9:07 pm

    I might be way off here-

    >>> def tokenize(source):
    ...     search = re.compile(r'^(.+?)(?:;(.+?))*?(?:#.+)?$', re.MULTILINE)
    ...     return (token.strip() for line in source.split('\n') if search.match(line)
    ...                   for token in line.split('#', 1)[0].split(';') if token)
    ... 
    >>> 
    >>> 
    >>> for token in tokenize('''\
    ... a = 1 + 2; b = a - 3 # create zero in b
    ... c = b * 4; d = 5 / c # trigger div error
    ... 
    ... e = (6 + 7) * 8
    ... # try a boolean operation
    ... f = 0 and 1 or 2
    ... a; b; c; e; f'''):
    ...     print(repr(token))
    ... 
    'a = 1 + 2'
    'b = a - 3'
    'c = b * 4'
    'd = 5 / c'
    'e = (6 + 7) * 8'
    'f = 0 and 1 or 2'
    'a'
    'b'
    'c'
    'e'
    'f'
    >>> 
    

    If applicable, I would keep the re.compile out of the def scope.

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

Sidebar

Related Questions

I have written this code to join ArrayList elements: Can it be optimized more?
I've written this very badly optimized C code that does a simple math calculation:
I need a more optimized version of this javascript code to find the 3
Here is an example that a forum poster gave, I can't tell if this
I have replicated a stripped-down version of my code that has recently been re-written
I'm curious about what be the best way to model this for optimized performance...
set(TestProject_additional_libs optimized foobar.lib debug foobard.lib } especially what is optimized/debug mean here? Is this
Just wondering if anyone can optimize this usortMonths() function to be better? Basically I
I have to implement an optimized version of malloc/realloc/free (tailored for my particular application).
I looked into the GCC STL (4.6.1) and saw that std::copy() uses an optimized

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.