Why do Python regex spans extend one place past the actual match? -


Looking at the spaces returned from my Regex matches, I saw that they always make a comeback behind the actual match; In the example such as

  & gt; & Gt; & Gt; Print (P. Match ('::: Message')) None & gt; & Gt; & Gt; M = p.search ('::: Message'); Print (m) & lt; 0x to _sre.SRE_Match object ... & gt; & Gt; & Gt; & Gt; M.group () 'Message' & gt; & Gt; & Gt;   

In the example (4, 11) vs. physical location (4, 10) . Due to the limitations of its left hand and right hand, the reason for my problem is different and I have to compare the span's relative positions.

Is there a good reason for this, or can I extend my preference to one by reducing the right limit?

Because in Python, slicing and range, the end value is never always unique, and '::: message' [4:11] indicates the actual matching text:

  & gt; & Gt; & Gt; '::: Message' [4:11] 'Message'   

In this way, you can match the result of MatchObject.span () results Khata Text Basic String:

  & gt; & Gt; & Gt; Import re & gt; & Gt; & Gt; S = '::: Message' & gt; & Gt; & Gt; Match = p. Search (s) & gt; & Gt; & Gt; MatchSpain () (4, 11) & gt; & Gt; & Gt; S [slice (* match. Span ())] 'message'    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -