python-re

Why do these two regular expressions work differently with re.sub(), but return the same match with re.search()?

Why do these two regular expressions work differently with re.sub(), but return the same match with re.search()? Question: Both regex are fetching same match but why does ",([d-]+)" work as expected and not the "(,d*-?d*-?d*)"? I was expecting both regex to give me same output during re.sub(). What am I missing? >>> print(re.search(r",([d-]+)", "Sabrina Green,802-867-5309,System …

Total answers: 1

re.sub a list of words, ignore case

re.sub a list of words, ignore case Question: I am trying to add the html <b> element to a list of words in a sentence. After doing some search I got it almost working, except the ignore-case. import re bolds = [‘test’, ‘tested’] # I want to bold these words, ignoring-case text = "Test lorem …

Total answers: 1

Remove texts that are enclosed in square brackets in Pandas

Remove texts that are enclosed in square brackets in Pandas Question: I would like to know how I can go through a column and remove the strings that are between square brackets. Example ‘String [more string]’ after ‘String’ Can anyone help me? I thought about using regular expressions. However, I don’t know how to zoom …

Total answers: 2

How to get the regex code for words spilted by underscore?

How to get the regex code for words spilted by underscore? Question: i have a problem in my project. WHAT I WANT TO DO ? i need to get all word split by underscore in python code: text = "i.am – M_o_h_a_m_m_e_d – and – _1_5_y_o – name – moh_mmed – 2_8_j – i___a_m" re.findall(r’?????’ …

Total answers: 3

Extracting specific string format of digits

Extracting specific string format of digits Question: Let us suppose we have text like this : text ="new notebook was sold 8 times before 13:30 in given shop" here we have 3 number presented, one is single digit 8 and last two are two digit numbers, 13,30, main point is , that 13:30 express time, …

Total answers: 3

How to parse custom operators inside a evaluable python string?

How to parse custom operators inside a evaluable python string? Question: Having a formula as a string like: str_forumla = "x > 0 AND y < 5 AND ‘this AND that’ in my_string" Where the python operator "&" have been substituted by "AND" (but the string AND was not affected) how to revert the operation …

Total answers: 2

Is there a simpler way in python

Is there a simpler way in python Question: I did the following to get from P13 to {P}{13} import re txt = "P13" x = re.search("[A-T]", txt ) y = re.split("[A-T]", txt ) txt = "{" + x.group() + "}" + "{" + y[1] + "}" It is working and gives what I want. But …

Total answers: 1

Expression that captures all characters up to a group of characters

Expression that captures all characters up to a group of characters Question: I have several alerts coming from a DC server, which have the following pattern: alert – name risk score – severity – total The examples of these alerts would be: A member was added to a security-enabled local group 47 medium 2 A …

Total answers: 2

How to extract span in re.finditer method in python?

How to extract span in re.finditer method in python? Question: the results of re.finditer is as below. [i for i in result] =[<re.Match object; span=(0, 10), match=’sin theta ‘>, <re.Match object; span=(12, 18), match=’cos x ‘>, <re.Match object; span=(20, 26), match=’e ^ x ‘>, <re.Match object; span=(26, 32), match=’f( x )’>, <re.Match object; span=(37, 45), …

Total answers: 2

Removing HTML Tag removes additional words

Removing HTML Tag removes additional words Question: I am working on a data cleaning problem wherein I have a task to remove HTML tags from string while keeping the content of text. Example text for cleanup is given below. I tried removing "pre" tags and somehow i do not get any data. x = ‘<pre>i …

Total answers: 2