I have seen many posts on this but I still can not work it, I do not know why.
I have some floating point and it has a relatively simple string with integer numbers such as: '2 1.000000000000000 1 1 0'
. I want to extract only the integer from it, in this example only 2, 1, 1, 0
( 1
is not that which is 0
S) .
I know that I will have to use the numbers that use lookbehind
and lookahead
to exclude > > > Code>. I can successfully find numbers that occur before a coma, in that case 0
:
import again fullCertain = recompackage ('- (? ? & Lt; = \.) \ D + (?!?) ') A =' 2 1.00000000000000000 1 1 0 'FullPortal Fondol (A)
Back to ['000000000000000']
, as I want but when I try to find the numbers which no < / Em> before .
s does not work:
Re-import IntegerPattern = re Compile ('- (?? (? & Lt ;! \.) \ D + (?! \ ) 'A' = '2 1.000000000000000 1 1' 'Integerparent Tern. Fundol (A)
Return [' 2 ',' 00000000000000 ',' 1 ',' 1 ' '0']
. Any ideas why? I am completely new to general expression and it just does not leave me. This should work but it is not.
(\ S | ^) \ d + (\ s | $)
The code can be
& Gt; & Gt; & Gt; N = '2 1.000000000000000 1 1 0' & gt; & Gt; & Gt; ('? (?: \ S | ^) \ d + (?: \ S | $)', n) ['2', '1', '0']
(\ s * | ^)
matches a location or the beginning of the string
\ d +
matches any number of digits
(\ s * | $)
matches the end of the location or string
Note: \ b
to < Code> \ d + / Code> as .
also \ b
example
edit
why include Regex (? & Lt ;! \.) \ D + (?!!)
work
The problem here is now that when the argument seems negative, then we match the match .
and regex .
When you type (? regex is a condition where it can be successful
He is saying 1.000000
regex condition fixes second 0
, as the previous state is not .
(which is zero) and the remaining 00000
is thus a winner. Therefore it matches this.
Check this link to get a clear view
As you 1 A more correct regex will be 1.00000000000000000 match
(? :(? & Lt; = ^) | (? & Lt; = \ s)) \ d + (? = \ S | $)
& Gt; & Gt; N '1 2 3 4.5' & gt; & Gt; & Gt; Re.findall (r '(?: (? & Lt; = ^) | (? & Lt; = \ s)) \ d + (? = \ S | $)', n) ['1', '2 ',' 3 ']> gt; & Gt; & Gt; N = '1 2 3 4' & gt; & Gt; & Gt; Re.findall (r '(?: (? & Lt; = ^) | (? & Lt; = \ s)) \ d + (? = \ S | $)', n) ['1', '2 Thank you for telling you
Comments
Post a Comment