Python re.split how to split on multiple patterns specified from variables

Advertisements I am trying to split a string on multiple sub strings, but I can not get re.split() to accept 2 variables as the patterns. I see multiple example of how to use individual characters or combinations of special characters as patterns, but nothing about using variables. These are dummy inputs. The strings will be… Read More Python re.split how to split on multiple patterns specified from variables

Matching and replace multiple strings in python

Advertisements import re text = ‘{"mob":"1154098431","type":"user","allowOrder":false,"prev":1}’ newstring = ‘"allowOrder":true,"’ #newstring = ‘"mob":"nonblocked",’ reg = ‘"allowOrder":(.*?),"|"mob":"(.*?)",’ r = re.compile(reg,re.DOTALL) result = r.sub(newstring, text) print (result) im trying to matching and replacing multiple regex patterns with exact values can someone help me to achieve this >Solution : You should parse the JSON rather than trying to use… Read More Matching and replace multiple strings in python

re.search in python, return a list of integers separated by comma

Advertisements I have a list of strings as follows: my_list = [‘array("i", [12694499, 12855016, 123457])’, ‘array("i", [12694499, 12865016, 123457])’, ‘array("i", [12694499, 12855016])’, ‘array("i", [12699749, 12877477])’, ‘array("i", [12828285, 12868277])’, ‘array("i", [-1])’, ‘array("i", [-1])’, ‘array("i", [-1])’] I am a newbie using regular expressions in python and I’m trying to use re.search to extract the values in brackets… Read More re.search in python, return a list of integers separated by comma

match multiple substrings using findall from re library

Advertisements I have a large array that contains strings with the following format in Python some_array = [‘MATH_SOME_TEXT_AND_NUMBER MORE_TEXT SOME_VALUE’, ‘SCIENCE_SOME_TEXT_AND_NUMBER MORE_TEXT SOME_VALUE’, ‘ART_SOME_TEXT_AND_NUMBER MORE_TEXT SOME_VALUE] I just need to extract the substrings that start with MATH, SCIENCE and ART. So what I’m currently using my_str = re.findall(‘MATH_.*? ‘, some_array ) if len(my_str) > 0:… Read More match multiple substrings using findall from re library