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

Why the program is not finding any email from the string?

The program should find all the emails from the string but it is returning nothing. my_str=""" Practice @Geeksforgeeksexpand_more Algorithmsexpand_more Data Structuresexpand_more Programming Languagesexpand_more Web Technologiesexpand_more Tutorial…
Tanish Sarmah
  • 430
  • 5
  • 14
0
votes
1 answer

Why doesn't this Regex match any of the dates?

I'm trying to match dates in a dataframe with 500 entries using regex: The dates can appear in the following formats: 04/20/2009; 04/20/09; 4/20/09; 4/3/09 Mar-20-2009; Mar 20, 2009; March 20, 2009; Mar. 20, 2009; Mar 20 2009; 20 Mar 2009; 20 March…
0
votes
1 answer

Python Read data from txt file using find all method for timestamps

I am reading data from txt file. It is listing the data between #### and ####. If there is same timestamps last two lines, workflow select the first one and separating it. When I print data in console(Print data:), you can see that there is a gap in…
nobody
  • 33
  • 4
0
votes
3 answers

How to create a regular expression that would find all pieces of text BETWEEN certain sets of characters?

I have a string that looks like 'E10 1/05/03 2/3211 3/AO Yuzhmor'. The pieces that i need to extract are the ones following ' \d\/': 1) 05/03 2) 3211 3) AO Yuzhmor My last idea was ' \d\/(.*?)(?=(( \d\/)|\Z))' but it still wouldn't work properly on…
0
votes
2 answers

Python regex, negate a set of characters in between a string

I have several set of strings with numbers followed words and jumbled numbers and words etc. For example, "Street 50 No 40", "5, saint bakers holy street", "32 Syndicate street" I am trying to separate the street names from the apartment…
Srivatsan
  • 9,225
  • 13
  • 58
  • 83
0
votes
0 answers

Why does there need to be backslash before dot?

Hello I have pattern for email adress and it works but I am struggling, why there need to be backslash before dot pattern = re.compile("[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
user14094104
0
votes
4 answers

Regular Expressions matching dates (greedy)

I have the following dates in a text file, 04/20/2009;04/20/09;4/20/09;4/3/09; Mar-20-2009;Mar 20, 2009;March 20, 2009;Mar. 20, 2009;Mar 20 2009; 20 Mar 2009;20 March 2009;20 Mar. 2009;20 March, 2009; Mar 20th, 2009;Mar 21st, 2009;Mar 22nd,…
imantha
  • 2,676
  • 4
  • 23
  • 46
0
votes
1 answer

re pattern matching with 3 options

This is the current code and I am trying to wrap my head around an alternative: Presently we have: URL_PREFIX = "http://ourrepo:8081/artifactory" pattern = re.compile(r'^.*-(ngwebui|nodeservice).*$') if pattern.match(artifact): return URL_PREFIX…
0
votes
2 answers

How can I remove special characters for just one column in a data frame?

I am trying to clean my data frame but I just want to remove special characters for just one column. (Please refer the figure below) df1 | A | B | C | |---------|----––|––----| | Ags(1) | 5 | 4 | | Cdmx(2) | 6 | 6 | |Leon(4)…
coding
  • 917
  • 2
  • 12
  • 25
0
votes
1 answer

Find text that matches no regex pattern in python

I have a bunch of moderately complicated patterns to match through a multi-GB log. I think between them all of the text matches some of the patterns, however I am curious if i'm missing something in the log. How can I efficiently find text that…
Ilya
  • 561
  • 2
  • 17
0
votes
1 answer

How to Achieve this $letters(*) with re Module in Python

I'm a total begginner when it comes to Regex , if any one has a suggestion for a good place to learn more about re Module, I would realy appreciate it . As for my question , Basically i'm playing around with it to find something like this…
Saad Amrani
  • 95
  • 2
  • 11
0
votes
1 answer

Regex to return single line when pattern is matched for a specific text file

I have multiple text files and wanted to extract the string when a specific pattern matches and append it in a data frame with the file name and the string. In my case multiple same patterns are present in these text files. sample.txt: "government…
SUBHRA SANKHA
  • 118
  • 1
  • 2
  • 11
0
votes
0 answers

Exponential running time for simple Python regex

In the case of this simple regex pattern = re.compile(r"(.*)*A") which is immediately compiled, the search time grows exponentially with string length. Is this an expected behavior of python's re library? I would expect that applying a compiled…
Peter Franek
  • 577
  • 3
  • 8
  • 25
0
votes
1 answer

Problem using re.sub with a replacement including Vertical lines

A Sample line of my data: 12808|08.12.2008|13:44:35|-0.05||||||||0.26|1.53|2.94|0.81|1.75|5.53|79.56||||2|K:\Path\to\File\TE08-08-Chla-12.08.2008.xls|19.01.2009 09:34:57|9|15|| The search patterns and…
0
votes
2 answers

How do I omit certain parts of a string with python's re?

I have this string: url = '/justicefor/404/1nirmala5.jpg' I want to extract it as 404.jpg. I tried something like: pattern = re.compile( r"./justicefor/(\d+/.\.\w+)", re.IGNORECASE ) But this selects the text between 404 and…
Saurav Pathak
  • 796
  • 11
  • 32
1 2 3
99
100