r1chardj0n3s / parse

Parse strings using a specification based on the Python format() syntax.
http://pypi.python.org/pypi/parse
MIT License
1.72k stars 101 forks source link

Suggestion :u to detect url #141

Closed ukgenes closed 3 months ago

ukgenes commented 2 years ago

{:u} to detect http[s}://kjjhh.com/asdhfk/sdfh/adsf?dsf=jkk

Great library!!!

jenisys commented 2 years ago

Wouldn't it be better to provide a readable name, like "{:Url}" or "{:url} instead of "{:u}" ?!?

NOTE THAT: You can already use that by registering your own type. You just need to figure out what the regex pattern should look like.

bendichter commented 1 year ago

Here's how you would do that yourself:

from parse import parse, with_pattern

regex_url = r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)"

@with_pattern(regex_url, regex_group_count=1)
def url(url):
    return url

parse("{:url}", "http://www.hello-world.com", dict(url=url)))