0

I have data as follows

[{"ruleId":"RULE","ruleName":"Dependency_rule","ruleDescription":"package rules ; import ava; rule \"M-LC-123 Dependency\" when accountO:Account ( !(Ids contains \"M-LC-456\") && !(Ids contains \"M-LC-789\") && !(Ids contains \"M-LC-91011\") && !(Ids contains \"M-LC-121314\") && !(Ids contains \"M-LC-1151617\") && !(Ids contains \"M-LC-181920\") && !(Ids contains \"M-LC-212223\")) then accO.setBlock(false); end ","ruleType":"DEPEND","ruleExpression":null}]

I want to extract all the values which start with M-LC from this. Example M-LC-123, M-LC-456 and rest all.

How can I achieve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Does this answer your question? [How do I get a substring of a string in Python?](https://stackoverflow.com/questions/663171/how-do-i-get-a-substring-of-a-string-in-python) – Björn Zurmaar May 21 '20 at 19:40

1 Answers1

1

a simple solution using regular expressions:

pattern = r"(M-LC-(\d+))" means "find every occurence of M-LC- followed by one or more digits"

import re

data = [{"ruleId":"RULE","ruleName":"Dependency_rule","ruleDescription":"package rules ; import ava; rule \"M-LC-123 Dependency\" when accountO:Account ( !(Ids contains \"M-LC-456\") && !(Ids contains \"M-LC-789\") && !(Ids contains \"M-LC-91011\") && !(Ids contains \"M-LC-121314\") && !(Ids contains \"M-LC-1151617\") && !(Ids contains \"M-LC-181920\") && !(Ids contains \"M-LC-212223\")) then accO.setBlock(false); end ","ruleType":"DEPEND","ruleExpression":None}]

data_str = str(data) # prevents need to iterate list/dicts
pattern = r"(M-LC-(\d+))"

results = [m.group(0) for m in re.finditer(pattern, data_str)]

print(results)

Output:

['M-LC-123', 'M-LC-456', 'M-LC-789', 'M-LC-91011', 'M-LC-121314', 'M-LC-1151617', 'M-LC-181920', 'M-LC-212223']
Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
  • amazing it works, can you please explain me the working of pattern and results line, for my understanding? –  May 21 '20 at 13:24
  • @Kimberly_Jennifer there's a lot to learn about regular expressions, I suggest reading the docs page I linked to. basically it's a way to search complex patterns in text, using a special syntax. e.g. `\d` means "any digit", and the `+` means "one or more times". – Adam.Er8 May 21 '20 at 13:29