I
I
intnzy2013-03-04 18:34:03
Python
intnzy, 2013-03-04 18:34:03

Why does the expression "fire"?

Program in Python.

line='/link/abcd'
pattern='/link/abcd(\/.*)?'
m=re.match(pattern,line)


Tell me why match works with line = '/link/abcdefffsd' for example?
It was assumed that it should work only on a link like
/link/abcd
/link/abcd/blablabla
i.e. either the link itself, or the link ends with / and then any characters after the slash.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
S
Sergey Pankov, 2013-11-16
@intnzy

You have an extra backslash in "\" and the string is not raw. In short, it is correct to write like this:
The tail slash is not required, but if it is, any characters in any number, including zero, are allowed after it.

T
truekenny, 2013-03-04
@truekenny

And if you put "$" at the end of the pattern? (without quotes)

R
Refandler, 2013-03-04
@Refandler

import re

line='/link/abcd/fdfs'
pattern='/link/abcd(\/.*)$'
m=re.match(pattern,line)
if (m):
  print "Okay"
else:
  print "WTF????"

So do it

R
Refandler, 2013-03-04
@Refandler

import re

line='/link/abcd'
pattern='^/link/(abcd)/(.*)$'
m=re.match(pattern,line)
if (m):
  print "Okay"
else:
  print "WTF????"

V
Vladimir Dubrovin, 2013-03-04
@z3apa3a

Works because of what? at the end (i.e. the part in brackets is optional). What you want is done with the regex
/link/abcd(/.*)?$

E
Evgeny Elizarov, 2013-03-04
@KorP

pattern='/link/abcd(\/.*)?$'

A
afiskon, 2013-03-06
@afiskon

The .* expression matches, among other things, the empty string. What you want to achieve, as I understand it, is (\/.+)? See also tynts .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question