Python regex to return characters after a search term

yahop19531

I'm trying to use regex to extract information from some strings

I can find the text using re.search, but how to get the information provided after the search term?

In the format Ref number ABC123456Ref date 01.01.2000 how can I return ABC123456 and 01.01.2000?

import re
reqd_info = "Ref number"
test_text = 'Ref number ABC123456Ref date 01.01.2000'
res = re.search(reqd_info, test_text)

Czaporka

You can use groups in your regular expression, then use re.findall to get a list of tuples containing the matched groups.

In [1]: import re

In [2]: test_text = 'Ref number ABC123456Ref date 01.01.2000'

In [3]: re.findall("Ref number (.*)Ref date (.*)", test_text)
Out[3]: [('ABC123456', '01.01.2000')]

In [4]: [[number, date]] = re.findall("Ref number (.*)Ref date (.*)", test_text)

In [5]: number
Out[5]: 'ABC123456'

In [6]: date
Out[6]: '01.01.2000'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Return last occurence of search term in python3

Python regex search for alphanumeric characters and forward slash

Python 3.6.1 | Regex Search on files with special characters

regex to find string that has all the previous characters before it, i.e. a final search term

how to search characters between parentheses and search for a named backreference regex python

python alternative regex search return tuple with None

Python - Return some characters after phrase in string

Regex : How would I use Negative Lookahead to exclude specific character that may come in front or after the search term

Elasticsearch: Term search not working on special characters

Get search term with regex pattern in an url

How to search for a term in a Dataframe and return Yes or no

How to search/find special characters like &, < or > in the string with regex using Python

Python: lst[(lst.index("search-term"))] = article in function body vs. return

How to add newline return after regex search before and after the regex pattern?

Wordpress search not working with search term exceeding 87 characters

Displaying everything after search term is deleted

Python regex that will require at least 6 characters to return true

How to negate filename after a specific term in a regex

Python Pandas Regex: Search for strings with a wildcard in a column and return matches

Python: Regex Search Special Characters giving errors while searching multiple characters

vim - don't highlight search term after a search & replace?

SQL Text Search - EXACT before space, LIKE after in search term

regex: return characters inside parenthensis

Ubuntu terminal grep escape all the special characters in the search term literal

Regex to search HTML and find a number after the occurrence of a string in python

How to return the link to the first Youtube video after a search in Selenium Python?

Is there a way to use regex to search for x amount of characters after a keyword but before it reaches '|'?

Regex, return first match after specific word / Python

"Spell check" and return the corrected term in Python

TOP Ranking

HotTag

Archive