user651521
user651521

Reputation: 5

match some string

I want to make a regex that match baking.asp nhdjdl.asp hdghgdh.asp hksks.asp

but not any file starting with http:// like

http://hhhdjg.asp

How can I make that since I do not know how to say except this sequence of character

Upvotes: 0

Views: 87

Answers (3)

Bryan Oakley
Bryan Oakley

Reputation: 385970

I think the best solution is to find all patterns that match '*.asp" , and from that throwing out any results that begin with "http:" . Since you don't know regular expressions (and presumably your teammates don't either, if you have them), a non-regexp solution will be the most clear.

For example:

[s for s in list_of_strings if s.endswith(".asp") and not s.startswith("http://")]

Upvotes: 0

kamae
kamae

Reputation: 1867

If you also expect to exclude string starting with ftp://, etc:

>>> re.match('[^/]*\.asp', '/tmp/foo.asp')
>>> re.match('[^/]*\.asp', 'http://foo.asp')
>>> re.match('[^/]*\.asp', 'ftp://foo.asp')
>>> re.match('[^/]*\.asp', 'foo.asp')
<_sre.SRE_Match object at 0x2abe856856b0>

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798636

Negative lookahead assertions.

>>> re.match('(?!http://).*\\.asp', 'http://foo.asp')
>>> re.match('(?!http://).*\\.asp', 'foo.asp')
<_sre.SRE_Match object at 0x7f34f8432920>

Upvotes: 3

Related Questions