Skip to content

Commit 097a89e

Browse files
authored
tag_line_pattern: support curly braces
merges #31
1 parent e3c85dc commit 097a89e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

obonet/read.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,22 @@ def get_sections(
105105

106106
# regular expression to parse key-value pair lines.
107107
tag_line_pattern = re.compile(
108-
r"^(?P<tag>.+?): *(?P<value>.+?) ?(?P<trailing_modifier>(?<!\\)\{.*?(?<!\\)\})? ?(?P<comment>(?<!\\)!.*?)?$"
108+
r"""^
109+
(?P<tag>.+?):\s* # tag and separator
110+
(?P<value>.*?) # value: match anything (non-greedy)
111+
(?:\s # optional trailing modifier
112+
(?P<trailing_modifier>
113+
(?<!\\)\{[^{}]*\} # match unescaped {...}
114+
)
115+
)?
116+
(?:\s
117+
(?P<comment> # optional comment
118+
(?<!\\)![^\n]* # match unescaped ! followed by any characters
119+
)
120+
)?
121+
\s*$ # optional trailing whitespace
122+
""",
123+
re.VERBOSE,
109124
)
110125

111126

tests/test_obo_reading.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ def test_parse_tag_line_backslashed_exclamation() -> None:
125125
assert value == r"not a real example \!"
126126

127127

128+
def test_parse_tag_line_curly_braces() -> None:
129+
"""Test that we can handle curly braces inside tag lines"""
130+
line = 'synonym: "10*3.{copies}/mL" EXACT [] {http://purl.obolibrary.org/something="AB"}'
131+
tag, value, trailing_modifier, comment = parse_tag_line(line)
132+
assert tag == "synonym"
133+
assert value == '"10*3.{copies}/mL" EXACT []'
134+
assert trailing_modifier
135+
136+
128137
def test_ignore_obsolete_nodes() -> None:
129138
"""Quick verification that the change doesn't break anything"""
130139
path = os.path.join(directory, "data", "brenda-subset.obo")

0 commit comments

Comments
 (0)