I'm making the string Lexer, but I don't know how to make it, and the letters after the last space don't come out, but I'm curious and don't know how to fix it.
The goal is to categorize the string in the list based on the space, but leave the space in the "characters in" or in the "space".
So when there's a code like this,
"Hello, world!" Output 'Hello, world!' Say 'Hello, world!'
["str:Hello, world!", "Output", "str:Hello, world!", "Speaking"]
How do I write Python code? For another example,
"Hello" output
["str: Hello", "Output"]
It's to switch to.
python programming-language
If this is the case, first of all, you should not divide the given full string input into spaces, but divide it by quotation criteria.
import re
regex = r"(['\"][^'\"]+['\"]) ?([^'\" ]*)"
subst = "\\g<2> \\g<1>"
test_str = "\"Hello, world!\" output Say 'Hello, world!'"
In fact, quotation marks are not important, but the task of making command-arguments pairs with a unique object-descript structure in Korean is probably more essential. If that's the real problem with this... I'd like to introduce some of the abbreviations.
# I haven't turned the code. Just look at the concept!
inputstring = input
commands = []
For keyword in ('Output', 'Speaking') :
pattern = r"(['\"][^'\"]+['\"]) ?" + keyword
If inputstring matched pattern:
commands.push
Remove just matched from inputstring
PS. By the way... What you're trying to do is...?
© 2024 OneMinuteCode. All rights reserved.