String pattern in Python [duplicate] - python

This question already has answers here:
How do I validate a date string format in python?
(5 answers)
Closed 6 months ago.
Im trying to check if a user's input is following the pattern integer/integer/integer(like month/day/year) but i dont know how to use exactly the match function to define that the pattern contains "number",then "/",again "number" and "/"...

Check out https://regex101.com/ for a neat website to check your regex! This is implemented in python using the re library. https://docs.python.org/3/library/re.html
In your case, the pattern would be [0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}

Related

Email Regex Validation fails in python [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I am using python to extract Emails from web using re library. it does its job but it extracts links that match the pattern. For example:
/images/paramproofs/services/pgp/logo_black_16#2x.png
/images/paramproofs/services/twitter/logo_black_16#2x.png
/images/paramproofs/services/github/logo_black_16#2x.png
/images/paramproofs/services/reddit/logo_black_16#2x.png
/images/paramproofs/services/web/logo_black_16#2x.png
/images/paramproofs/services/web/logo_black_16#2x.png
/images/paramproofs/services/stellar/logo_black_16#2x.png
/images/badges/install-badge-windows-168-56#2x.png
/images/badges/install-badge-windows-168-56#3x.png
This is the pattern I use:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[ a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
I don't know where you took that regex from, but according to emailregex.com this should suffice for almost all cases (including yours):
(^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)
The line anchors (^ for the beginning of the line and $ for the end) are the key here.

Numeric pattern search in regular expression using Python [duplicate]

This question already has answers here:
How to use regex to find all overlapping matches
(5 answers)
Closed 2 years ago.
I have text as below-
my_text = "My telephone number is 408-555-1234"
on which i am searching the pattern
re.findall(r'\d{3}-\d{1,}',my_text)
My intention was to search for three digit numeric value followed by - and then another set of one or more than one digit numeric value. Hence I was expecting the result to be - ['408-555','555-1234'],
However the result i am getting os only ['408-555'] .
Could anyone suggest me what is wrong in my understaning here. And suggest a pattern that would serve my purpose
you can use:
re.findall(r'(?=(\d{3}-\d+))', my_text)
output:
['408-555', '555-1234']

Retrieve part of string after underscore [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 3 years ago.
I know that in Python you can use the array selector to retrieve a certain part of a string, ie me.name[10:] to get just the last 10 characters.
but how would you retrieve just the part of a string after an underscore ie _ using a single expression?
For example if my string is "stringcharThatChange_myname"
How would I extract just 'myname' ? I'm confined to using Python 3.5.1
You could use split.
test_string = "stringcharThatChange_myname"
print(test_string.split('_')[1]) # myname
Using split method.
"this will be excluded_this is kept".split('_')[1]

Python regex with multiple matches in the same string [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Python non-greedy regexes
(7 answers)
Closed 3 years ago.
test = '<tag>part1</tag><tag can have random stuff here>part2</tag>'
print(re.findall("<tag.*>(.*)</tag>", test))
It outputs:
['part2']
The text can have any amount of "parts". I want to return all of them, not only the last one. What's the best way to do it?
You could change your .* to be .*? so that they are non-greedy. That will make your original example work:
import re
test = '<tag>part1</tag><tag can have random stuff here>part2</tag>'
print(re.findall(r'<tag.*?>(.*?)</tag>', test))
Output:
['part1', 'part2']
Though it would probably be best to not try to parse this with just regex, but instead use a proper HTML parser library.

Using regular expressions to extract certain text from a string in Python? [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 8 years ago.
I am scraping data from a website and the tag contains the text 'ANSWER (1)'.
My goal is to extract just the '1' from the string using regular expressions in python.
Is there a simple way to go about doing this?
Try this code:
import re
s = "ANSWER (1)"
print re.search(r".*\((\d)\)",s).group(1)

Categories

Resources