Python Forum

Full Version: regular expression
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Str1 = [Math1] Total marks [ End Math1] [Data] Total data [ End Data ]
I am trying to extract the paragraph like using re.

json =
{"Math1":[
]
"Data":[
]

Thanks.
Does your string format allow nesting? E.g. Can one data block include math block or data block? If so, you are likely needed a parser and the problem cannot be solved using one or two specific regular expressions only. However, regular expressions can help you to implement the parser.
One hacky way is to use regular expressions to replace [math] [end math] and data blocks to something like <math count="1"> </math>, to turn your string into valid xml-string and use, e.g. lxml library to parse it and extract the data.
Also, take a look at pyparsing module.
input_text = "system computer laptop"
input_text = "mobile sim card"

We retrieve first:
text = "system computer laptop"

how do we match using import re

output = re.search(input_text, text)
print(output)

case ii.  we need to match text = "mobile sim card" and print it "mobile sim card"
output = re.search(input_text, text)
print(output)