is it possible? for example the capture group in the pattern '(ab)' wouldn't consume characters in the string 'ab' somehow?
Asked
Active
Viewed 126 times
-4
-
do you mean non-capturing group? – Ali Yılmaz Jan 19 '19 at 22:59
-
https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-what-does-do – Ali Yılmaz Jan 19 '19 at 22:59
-
Use lookaround instead – CertainPerformance Jan 19 '19 at 22:59
-
no i actually want to capture some characters - but then not consume the characters i just captured – DyRuss Jan 19 '19 at 23:00
-
@CertainPerformance yes that is what i want, but i think lookaround will discard the regex match? i want to keep the match – DyRuss Jan 19 '19 at 23:02
-
No, lookaround will not discard captured groups. – CertainPerformance Jan 19 '19 at 23:04
-
Can you give an example with the input, pattern, and desired output? – Josh Withee Jan 19 '19 at 23:24
1 Answers
0
A people have said in the comments a look ahead can capture but not consume. The following will capture the letter B into both capture groups even though it only exists once.
import re
matched = re.search('(?=(ab)).(bc)','12abc345')
if matched: print matched.groups()
OUTPUT
('ab', 'bc')
However for future posts it would be nice to see an attempt at some code or at least a minimum example of code that doesnt work for you. Example input and example output. So since you question lacked significant detail all people can do is post a generic example for you. If thats not enough for you to work from then you would need to update your question with more information about what you are trying to achieve.

Chris Doyle
- 10,703
- 2
- 23
- 42