Find the indexes of all regex matches?
I'm parsing strings that could have any number of quoted strings inside them (I'm parsing code, and trying to avoid PLY). I want to find out if a substring is quoted, and I have the substrings index. My initial thought was to use re to find all the matches and then figure out the range of indexes they represent.
It seems like I should use re with a regex like "[^"]+"|'[^']+'
(I'm avoiding dealing with triple quoted and such strings at the moment). When I use findall() I get a list of the matching strings, which is somewhat nice, but I need indexes.
My substring might be as simple as c
, and I need to figure out if this particular c
is actually quoted or not.
python regex
add a comment |
I'm parsing strings that could have any number of quoted strings inside them (I'm parsing code, and trying to avoid PLY). I want to find out if a substring is quoted, and I have the substrings index. My initial thought was to use re to find all the matches and then figure out the range of indexes they represent.
It seems like I should use re with a regex like "[^"]+"|'[^']+'
(I'm avoiding dealing with triple quoted and such strings at the moment). When I use findall() I get a list of the matching strings, which is somewhat nice, but I need indexes.
My substring might be as simple as c
, and I need to figure out if this particular c
is actually quoted or not.
python regex
Sounds like the job not suitable for regexes.
– Daniel Kluev
Aug 19 '10 at 7:19
add a comment |
I'm parsing strings that could have any number of quoted strings inside them (I'm parsing code, and trying to avoid PLY). I want to find out if a substring is quoted, and I have the substrings index. My initial thought was to use re to find all the matches and then figure out the range of indexes they represent.
It seems like I should use re with a regex like "[^"]+"|'[^']+'
(I'm avoiding dealing with triple quoted and such strings at the moment). When I use findall() I get a list of the matching strings, which is somewhat nice, but I need indexes.
My substring might be as simple as c
, and I need to figure out if this particular c
is actually quoted or not.
python regex
I'm parsing strings that could have any number of quoted strings inside them (I'm parsing code, and trying to avoid PLY). I want to find out if a substring is quoted, and I have the substrings index. My initial thought was to use re to find all the matches and then figure out the range of indexes they represent.
It seems like I should use re with a regex like "[^"]+"|'[^']+'
(I'm avoiding dealing with triple quoted and such strings at the moment). When I use findall() I get a list of the matching strings, which is somewhat nice, but I need indexes.
My substring might be as simple as c
, and I need to figure out if this particular c
is actually quoted or not.
python regex
python regex
edited Nov 14 '18 at 22:49
martineau
68.3k1090183
68.3k1090183
asked Aug 19 '10 at 7:14
xitriumxitrium
2,35131716
2,35131716
Sounds like the job not suitable for regexes.
– Daniel Kluev
Aug 19 '10 at 7:19
add a comment |
Sounds like the job not suitable for regexes.
– Daniel Kluev
Aug 19 '10 at 7:19
Sounds like the job not suitable for regexes.
– Daniel Kluev
Aug 19 '10 at 7:19
Sounds like the job not suitable for regexes.
– Daniel Kluev
Aug 19 '10 at 7:19
add a comment |
1 Answer
1
active
oldest
votes
This is what you want: (source)
re.finditer(pattern, string[, flags])
Return an iterator yielding MatchObject instances over all
non-overlapping matches for the RE pattern in string. The string is
scanned left-to-right, and matches are returned in the order found. Empty
matches are included in the result unless they touch the beginning of
another match.
You can then get the start and end positions from the MatchObjects.
e.g.
[(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]
Awesome! That works nicely. Thank you.
– xitrium
Aug 19 '10 at 7:24
30
Note that you can actually usem.span()
to get(m.start(), m.end())
(and the default group argument is0
, so that can be omitted).
– Amber
Mar 13 '11 at 5:08
1
Brilliant. Was looking for exactly this.
– armandino
Jul 9 '12 at 14:49
1
attention, it fails in this case: base_str = "GATATATGCATATACTT" sub_str = "ATAT", the result should be [(1,5), (3, 7), (9, 13)], but it turns out [(1, 5), (9, 13)]
– unionx
Dec 7 '14 at 16:39
3
If you want overlapping matches: stackoverflow.com/a/18966891/2300708
– Chris Chambers
Jul 9 '15 at 19:47
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3519565%2ffind-the-indexes-of-all-regex-matches%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is what you want: (source)
re.finditer(pattern, string[, flags])
Return an iterator yielding MatchObject instances over all
non-overlapping matches for the RE pattern in string. The string is
scanned left-to-right, and matches are returned in the order found. Empty
matches are included in the result unless they touch the beginning of
another match.
You can then get the start and end positions from the MatchObjects.
e.g.
[(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]
Awesome! That works nicely. Thank you.
– xitrium
Aug 19 '10 at 7:24
30
Note that you can actually usem.span()
to get(m.start(), m.end())
(and the default group argument is0
, so that can be omitted).
– Amber
Mar 13 '11 at 5:08
1
Brilliant. Was looking for exactly this.
– armandino
Jul 9 '12 at 14:49
1
attention, it fails in this case: base_str = "GATATATGCATATACTT" sub_str = "ATAT", the result should be [(1,5), (3, 7), (9, 13)], but it turns out [(1, 5), (9, 13)]
– unionx
Dec 7 '14 at 16:39
3
If you want overlapping matches: stackoverflow.com/a/18966891/2300708
– Chris Chambers
Jul 9 '15 at 19:47
|
show 2 more comments
This is what you want: (source)
re.finditer(pattern, string[, flags])
Return an iterator yielding MatchObject instances over all
non-overlapping matches for the RE pattern in string. The string is
scanned left-to-right, and matches are returned in the order found. Empty
matches are included in the result unless they touch the beginning of
another match.
You can then get the start and end positions from the MatchObjects.
e.g.
[(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]
Awesome! That works nicely. Thank you.
– xitrium
Aug 19 '10 at 7:24
30
Note that you can actually usem.span()
to get(m.start(), m.end())
(and the default group argument is0
, so that can be omitted).
– Amber
Mar 13 '11 at 5:08
1
Brilliant. Was looking for exactly this.
– armandino
Jul 9 '12 at 14:49
1
attention, it fails in this case: base_str = "GATATATGCATATACTT" sub_str = "ATAT", the result should be [(1,5), (3, 7), (9, 13)], but it turns out [(1, 5), (9, 13)]
– unionx
Dec 7 '14 at 16:39
3
If you want overlapping matches: stackoverflow.com/a/18966891/2300708
– Chris Chambers
Jul 9 '15 at 19:47
|
show 2 more comments
This is what you want: (source)
re.finditer(pattern, string[, flags])
Return an iterator yielding MatchObject instances over all
non-overlapping matches for the RE pattern in string. The string is
scanned left-to-right, and matches are returned in the order found. Empty
matches are included in the result unless they touch the beginning of
another match.
You can then get the start and end positions from the MatchObjects.
e.g.
[(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]
This is what you want: (source)
re.finditer(pattern, string[, flags])
Return an iterator yielding MatchObject instances over all
non-overlapping matches for the RE pattern in string. The string is
scanned left-to-right, and matches are returned in the order found. Empty
matches are included in the result unless they touch the beginning of
another match.
You can then get the start and end positions from the MatchObjects.
e.g.
[(m.start(0), m.end(0)) for m in re.finditer(pattern, string)]
edited Feb 15 '13 at 13:12
Balthazar Rouberol
3,96222439
3,96222439
answered Aug 19 '10 at 7:22
Dave KirbyDave Kirby
19.2k44976
19.2k44976
Awesome! That works nicely. Thank you.
– xitrium
Aug 19 '10 at 7:24
30
Note that you can actually usem.span()
to get(m.start(), m.end())
(and the default group argument is0
, so that can be omitted).
– Amber
Mar 13 '11 at 5:08
1
Brilliant. Was looking for exactly this.
– armandino
Jul 9 '12 at 14:49
1
attention, it fails in this case: base_str = "GATATATGCATATACTT" sub_str = "ATAT", the result should be [(1,5), (3, 7), (9, 13)], but it turns out [(1, 5), (9, 13)]
– unionx
Dec 7 '14 at 16:39
3
If you want overlapping matches: stackoverflow.com/a/18966891/2300708
– Chris Chambers
Jul 9 '15 at 19:47
|
show 2 more comments
Awesome! That works nicely. Thank you.
– xitrium
Aug 19 '10 at 7:24
30
Note that you can actually usem.span()
to get(m.start(), m.end())
(and the default group argument is0
, so that can be omitted).
– Amber
Mar 13 '11 at 5:08
1
Brilliant. Was looking for exactly this.
– armandino
Jul 9 '12 at 14:49
1
attention, it fails in this case: base_str = "GATATATGCATATACTT" sub_str = "ATAT", the result should be [(1,5), (3, 7), (9, 13)], but it turns out [(1, 5), (9, 13)]
– unionx
Dec 7 '14 at 16:39
3
If you want overlapping matches: stackoverflow.com/a/18966891/2300708
– Chris Chambers
Jul 9 '15 at 19:47
Awesome! That works nicely. Thank you.
– xitrium
Aug 19 '10 at 7:24
Awesome! That works nicely. Thank you.
– xitrium
Aug 19 '10 at 7:24
30
30
Note that you can actually use
m.span()
to get (m.start(), m.end())
(and the default group argument is 0
, so that can be omitted).– Amber
Mar 13 '11 at 5:08
Note that you can actually use
m.span()
to get (m.start(), m.end())
(and the default group argument is 0
, so that can be omitted).– Amber
Mar 13 '11 at 5:08
1
1
Brilliant. Was looking for exactly this.
– armandino
Jul 9 '12 at 14:49
Brilliant. Was looking for exactly this.
– armandino
Jul 9 '12 at 14:49
1
1
attention, it fails in this case: base_str = "GATATATGCATATACTT" sub_str = "ATAT", the result should be [(1,5), (3, 7), (9, 13)], but it turns out [(1, 5), (9, 13)]
– unionx
Dec 7 '14 at 16:39
attention, it fails in this case: base_str = "GATATATGCATATACTT" sub_str = "ATAT", the result should be [(1,5), (3, 7), (9, 13)], but it turns out [(1, 5), (9, 13)]
– unionx
Dec 7 '14 at 16:39
3
3
If you want overlapping matches: stackoverflow.com/a/18966891/2300708
– Chris Chambers
Jul 9 '15 at 19:47
If you want overlapping matches: stackoverflow.com/a/18966891/2300708
– Chris Chambers
Jul 9 '15 at 19:47
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3519565%2ffind-the-indexes-of-all-regex-matches%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sounds like the job not suitable for regexes.
– Daniel Kluev
Aug 19 '10 at 7:19