0

Python regular expresion

what does "(\d)(\\1*)" mean? I know what (\d) means , but what is the (\\1*)????

26th Apr 2017, 12:51 PM
고혁인
8 Antworten
+ 7
(\\1*) means that we are looking for a string beginning with "\" and followed by 0 or more repetitions of 1. Are you sure you didn't want to look for a repetition of a group you first created? Cause that's what the single backslash would do. And since it is double (escaped), it means what I wrote earlier. EDIT: Turns out the strings are not raw in this case and this is why the backslash has to be escaped. So We have /d for any decimal digit and zero or more repetitions of the first group.
26th Apr 2017, 2:07 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
* marks zero or more occurences (in this case repetitions). I checked around and yes, you have to use double backslash if tackling non-raw strings: https://docs.python.org/3/library/re.html#raw-string-notation So we have our little situation explained :)
26th Apr 2017, 6:42 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
Hmm... By using \1, \2 and so on, you address the first, second, etc. group of regex. So this is what I assumed you wanted to achieve.
26th Apr 2017, 6:34 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
The first parentheses looks for any decimal digit. If the second one is expected to check for the repetition of the first group zero or any number of times, the only explanation of the double backslash I can think of is that the string which is checked is not raw - then you probably have to escape it, indeed.
26th Apr 2017, 6:26 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
import re >>> curr = '1' >>> for each in range(30): ... match = re.findall("(\d)(\\1*)",curr) ... curr = "".join([str(len(k+y))+k for k,y in match]) ... >>> print len(curr) \\ 1,11,21,1211,111221, ... \\ 5808 In this program,what does the "(\d)(\\1*)" mean???
26th Apr 2017, 6:24 PM
고혁인
0
Isn` it the 0 or more repitition of first group "(\d)*" ???
26th Apr 2017, 6:31 PM
고혁인
0
1, 11, 21, 1211, 111221, •••• have no `\` Then how can this regular expression (\d)(\\1*) make the next item?
26th Apr 2017, 6:33 PM
고혁인
0
then what is the * after 1?
26th Apr 2017, 6:37 PM
고혁인