Find the indexes of all regex matches?










48















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.










share|improve this question
























  • Sounds like the job not suitable for regexes.

    – Daniel Kluev
    Aug 19 '10 at 7:19















48















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.










share|improve this question
























  • Sounds like the job not suitable for regexes.

    – Daniel Kluev
    Aug 19 '10 at 7:19













48












48








48


17






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















107














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)]





share|improve this answer

























  • Awesome! That works nicely. Thank you.

    – xitrium
    Aug 19 '10 at 7:24






  • 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






  • 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











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
);



);













draft saved

draft discarded


















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









107














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)]





share|improve this answer

























  • Awesome! That works nicely. Thank you.

    – xitrium
    Aug 19 '10 at 7:24






  • 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






  • 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
















107














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)]





share|improve this answer

























  • Awesome! That works nicely. Thank you.

    – xitrium
    Aug 19 '10 at 7:24






  • 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






  • 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














107












107








107







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)]





share|improve this answer















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)]






share|improve this answer














share|improve this answer



share|improve this answer








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 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





    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






  • 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






  • 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




















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto