Questions tagged [python-re]

Python library that provides regular expression matching operations similar to those found in Perl.

re is the Python built-in module to deal with regular-expressions. It offers an intuitive, high-level mechanism to match patterns on strings.

The main functions to use from this module are:

  • re.compile - this function takes a pattern and some possible flags and returns a Pattern object. This is mostly useful when using the same pattern in a loop - compile the pattern once before the loop, instead of at each iteration.

  • re.match - takes a pattern and a string (and possible flags) and tries to match the pattern from the beginning of the string. Returns a Match object.

  • re.search - similar to match, but searches anywhere in the string.

  • re.findall - similar to search, but returns a list with all matches found. The list contains strings rather than Match objects. When the pattern contains groups, the list will consist of tuples containing the groups of each match.

The re module also offers a regex-equivalent replacements for the built-in split - re.split - and replace - re.sub.

1981 questions
0
votes
2 answers

finding a word before a specific string with regex from a text file

i am very new to regex world. what i have is a text file which i would like to find a specific word before a specific string (in this case 'out') in it and store into a variable. so i can replace it with something else later in the code. below i…
Ahmad_R
  • 62
  • 1
  • 8
0
votes
2 answers

How to split a string into text and number using re library or any other method in an Excel sheet?

I need to convert the first column of the Excel sheet into an integer value. Need to remove the string (say LP001005, remove LP and get the rest of the number). I am able to achieve this on a single variable. But, I need to achieve this on the Excel…
Shubhra Garg
  • 167
  • 1
  • 3
  • 14
0
votes
2 answers

how to enclose sub-string into square brackets python

I have a very long text with parts enclosed in +++ which I would like to enclose in square brackets se1 = "+++TEXT:+++ Moshe Morgenstern is on his way to the main synagogue in the center of Bnei Brak, home to a largely ultra-orthodox - or haredi -…
user343
  • 21
  • 8
0
votes
2 answers

python regex to get particular word

I am new to python and I am not familiar with regex pattern. I am using re package to get particular text in my code. but it doesn't seems to work. please help! import re text = '
1.sh'

filename = re.match(r'\D+="[*]"\D',…
Jack
  • 1
  • 1
0
votes
3 answers

Using regex to extract characters either side of a match

I have a string: test=' 40 virtual asset service providers law, 2020e section 1 c law 14 of 2020 page 5 cayman islands' I want to match all occurrences of a digit, then print not just the digit but the three characters either side of the…
agorapotatoes
  • 311
  • 2
  • 16
0
votes
1 answer

Question on regex from course using python to interact with operating system on coursera

Fill in the code to check if the text passed includes a possible U.S. zip code, formatted as follows: exactly 5 digits, and sometimes, but not always, followed by a dash with 4 more digits. The zip code needs to be preceded by at least one space,…
0
votes
3 answers

Creating dictionary and its key- value from line of log-file

I'm trying to do following, if I have the following ERROR line in log file: Aug 9 12:44:39 hostnameABC gnome-terminal-[12581]: Theme parsing error: gtk.css:6765:28: Missing opening bracket in color definition i need to end up with a dictionary…
0
votes
2 answers

How to read line and move on to the next line if the identifiers are not present?

in the following code I get an error every time the line doesn't have each of the three identifiers. How can I skip the line and move to the next if the identifiers are not present in the file? If the first line does not have mfgcode, modelno, and…
mjbaybay7
  • 99
  • 5
0
votes
1 answer

write in a file.txt in the flask server page

Here is the code i execute in a normal python script it works, but when it comes to a flask function he create the file.txt, but with no writing in it app = Flask(__name__) @app.route('/execute',methods=['POST']) def execute(): message =…
Moun
  • 325
  • 2
  • 16
0
votes
1 answer

Replace substrings in a string using re.sub?

I need some advice to get my regular expression working as expected. It is an extended question to this question. I want to replace all passwords and keys with "xxxx" and "zzzz". import re d = {"password": "xxxx", "secret-key": "zzzz"} def…
user5580578
  • 1,134
  • 1
  • 12
  • 28
0
votes
1 answer

Python SSL Wrong Version Number for webpage urllib request

I am running a request to get IP addresses from a website but keep running into a problem half of the time where it gives the following error. It tells me that my SSL is the wrong version, I have searched all over the web but haven't found anything…
0
votes
1 answer

Mask multiple sensitive data using re.sub?

I would like to mask several values ​​in a string. This call works for a single value as expected. message = "my password=secure and my private_key=securekey should not be logged." message = re.sub(r"(?is)password=.+", "password=xxxx",…
user5580578
  • 1,134
  • 1
  • 12
  • 28
0
votes
1 answer

Python and Regex: Problem with re findall()

This is a project found @ https://automatetheboringstuff.com/2e/chapter7/ It searches text on the clipboard for phone numbers and emails then copy the results to the clipboard again. If I understood it correctly, when the regular expression contains…
Patux
  • 37
  • 6
0
votes
0 answers

error: bad character in group name '(normal_date)' at position 4

I am trying to use regular expression to find date in dd/mm/yyyy format.This is the expression i used. normal_date=r'(?P<(normal_date)>\d{1,2}\/\d{1,2}\/\d{4})' But i am getting an error: bad character in group name '(normal_date)' at position 4.I…
0
votes
1 answer

Converting text file to YAML in Python

I have a text file to convert to YAML format. Here are some notes to describe the problem a little better: The sections within the file have a different number of subheadings to each other. The values of the subheadings can be any data type (e.g.…
mj_whales
  • 124
  • 2
  • 11
1 2 3
99
100