regexp qns

E

eight02645999

hi
suppose i have a string like

test1?test2t-test3*test4*test5$test6#test7*test8

how can i construct the regexp to get test3*test4*test5 and
test7*test8, ie, i want to match * and the words before and after?
thanks
 
J

James Stroud

hi
suppose i have a string like

test1?test2t-test3*test4*test5$test6#test7*test8

how can i construct the regexp to get test3*test4*test5 and
test7*test8, ie, i want to match * and the words before and after?
thanks


py> import re
py> s = 'test1?test2t-test3*test4*test5$test6#test7*test8'
py> r = re.compile(r'(test\d(?:\*test\d)+)')
py> r.findall(s)
['test3*test4*test5', 'test7*test8']

James
 
E

eight02645999

James said:
hi
suppose i have a string like

test1?test2t-test3*test4*test5$test6#test7*test8

how can i construct the regexp to get test3*test4*test5 and
test7*test8, ie, i want to match * and the words before and after?
thanks


py> import re
py> s = 'test1?test2t-test3*test4*test5$test6#test7*test8'
py> r = re.compile(r'(test\d(?:\*test\d)+)')
py> r.findall(s)
['test3*test4*test5', 'test7*test8']

James

thanks !
I check the regexp doc it says:
"""
(?:...)
A non-grouping version of regular parentheses. Matches whatever
regular expression is inside the parentheses, but the substring matched
by the group cannot be retrieved after performing a match or referenced
later in the pattern.
"""
but i could not understand this : r'(test\d(?:\*test\d)+)'. which
parenthesis is it referring to? Sorry, could you explain the solution ?
thanks
 
G

Gabriel Genellina

hi
suppose i have a string like

test1?test2t-test3*test4*test5$test6#test7*test8

how can i construct the regexp to get test3*test4*test5 and
test7*test8, ie, i want to match * and the words before and after?
thanks

I suppose this is just an example and you mean "any word" instead of test1,
test2, etc.
So your pattern would be: word*word*word*word, that is, word* repeated many
times, followed by another word.
To match a word we'll use "\w+", to match an * we have to use "\*" (it's a
special character)
So the regexp would be: "(\w+\*)+\w+"
Since we are not interested in the () as a group by itself -it was just to
describe the repeating pattern- we change it into a non-grouping
parenthesis.
Final version: "(?:\w+\*)+\w+"

import re
rexp = re.compile(r"(?:\w+\*)+\w+")
lines = [
'test1?test2t-test3*test4*test5$test6#test7*test8',
'test1?test2t-test3*test4$test6#test7_test8',
'test1?nada-que-ver$esto.no.matchea',
'test1?test2t-test3*test4*',
'test1?test2t-test3*test4',
'test1?test2t-test3*',
]

for line in lines:
print line
for txt in rexp.findall(line):
print '->', txt

Test it with some corner cases and see if it does what you expect: no "*",
starting with "*", ending with "*", embedded whitespace before and after the
"*", whitespace inside a word, the very definition of "word"...
 
J

James Stroud

James said:
hi
suppose i have a string like

test1?test2t-test3*test4*test5$test6#test7*test8

how can i construct the regexp to get test3*test4*test5 and
test7*test8, ie, i want to match * and the words before and after?
thanks


py> import re
py> s = 'test1?test2t-test3*test4*test5$test6#test7*test8'
py> r = re.compile(r'(test\d(?:\*test\d)+)')
py> r.findall(s)
['test3*test4*test5', 'test7*test8']

James


thanks !
I check the regexp doc it says:
"""
(?:...)
A non-grouping version of regular parentheses. Matches whatever
regular expression is inside the parentheses, but the substring matched
by the group cannot be retrieved after performing a match or referenced
later in the pattern.
"""
but i could not understand this : r'(test\d(?:\*test\d)+)'. which
parenthesis is it referring to? Sorry, could you explain the solution ?
thanks

The outer parentheses are the grouping operator. These are saved and
accessible from a match object via group() or groups() methods. The "\d"
part matches a single digit 0-1. The (?:....) construct is used to make
a non-grouping operator that is not itself remembered for access through
the group() or groups() methods. The expression can also reference
earlier groups, but not groups specified with the non-grouping operator.

You may want to note that this is the most specific regular expression
that would match your given example.

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top