return 0
return 0

Reputation: 4366

Python regex match list but not list with dictionaries

I have text file that looks like

logSet1 = [
                       { log1 = event1,
                         log2 = event2
}
]


logSet2 = [
                         log1,
                         log2
]

I want to write a regex that match only logSet2 not logSet1. I have tried

\= \[[ ]*\n([^\{].*\n)*?[ ]*\]\

but it matches to both.. I thought [^\{] would force it to match with the string that does not begin with \{.

Upvotes: 1

Views: 55

Answers (1)

Tristan Bodding-Long
Tristan Bodding-Long

Reputation: 280

If your file is programmatically structured consider loading it into data structures and navigating those.

For your regex itself there are some features of the language you may want to be more comfortable with.

"[ ]*" -- any number of spaces is not great for what you're angling for. Tabs and non breaking white spaces will through you for a loop, as will system differences (\n vs \r vs \r\n). Try just \s*

\w is also your friend (word characters, a-zA-Z0-9 and some punctuation)

In that vein I'd propose:

\w[\w\s]*= \[\s*[\w,\s]*\]

A word character, followed by as many word or spaces, followed by an =, a space and the open brace. "\w[\w\s]*= [" gets you "logSet2 = [". Then as many whitespace characters, followed by word characters, commas and spaces and finally terminated by a bracket. logSet1 with the curly brace and = will never match.

The drawback is that is the data, here represented as "log1" and "log2" have these characters the whole regex approach falls apart.

Upvotes: 2

Related Questions