regex fix

This commit is contained in:
bk_office
2026-04-27 21:18:05 +08:00
parent cf52ea683b
commit 733114b941
8 changed files with 58 additions and 48 deletions

View File

@ -16,6 +16,8 @@ example:
from dataclasses import dataclass
import regex as re
from dataclasses import dataclass
@dataclass
class Command:
id: str
@ -40,8 +42,10 @@ class CommandChainParser:
class CommandParser:
CMD_PATTERN = r"^(([a-zA-Z0-9]+(?:\(([^()]*|(?1)+)(\s*\,\s*(?1))*\))?))"
CMD_PATTERN = r"^[a-zA-Z]+(?:\(([0-9.]+|(([a-zA-Z0-9]+(?:\(([^()]*|(?1)+)(\s*\,\s*(?1))*\))?)(\.(?1))*))(\s*\,\s*(?1))*\))?"
ID_PATTERN = r"^[a-zA-Z]+(?=\(|\.|$)"
ARG_PATTERN = CommandChainParser.CHAIN_PATTERN
TRIM_PATTERN = r"^\s*\,\s*"
def __init__(self, _command: str) -> None:
self.command = _command
@ -53,10 +57,17 @@ class CommandParser:
if query := re.match(CommandParser.CMD_PATTERN, self.command):
cmd = query[0]
if cmd_id_qry := re.match(CommandParser.ID_PATTERN, cmd):
cmd_id = cmd_id_qry[0]
id = cmd_id_qry[0]
cmd_args = cmd[len(id) + 1 : -1] # .replace(" ", "").split(",")
args: list[str] = []
self.command = self.command[len(cmd) + 1 :]
cmd_args = cmd[len(cmd_id) + 1 : -1]#.replace(" ", "").split(",")
while cmd_arg_qry := re.match(CommandParser.ARG_PATTERN, cmd_args):
arg = cmd_arg_qry[0]
args.append(arg)
cmd_args = cmd_args[len(arg) :]
if trim_qry := re.match(CommandParser.TRIM_PATTERN, cmd_args):
cmd_args = cmd_args[len(trim_qry[0]) :]
# while "" in cmd_args:
# cmd_args.remove("")
return Command(cmd_id, cmd_args)
raise StopIteration
# cmd_args.remove("")
return Command(id, args)
raise StopIteration