Regex with DOTALL, how to make it stop at some point?

deltaskelta

I have a python regex like this:

re.compile(r'(\[chartsjs\].*\[/chartsjs\])', re.DOTALL)

I am trying to do a re.findall on patterns like this:

[charts]
name: mychart
type: line
labels: fish, cat, dog
data: 4, 5, 6
data2:5, 7, 9
[/charts]

this is some text

[charts]
name: second
type: line
labels: 100, 500, 1000
data: 50, 100, 10000
data2: 100, 100, 100
[/charts]

But it seems like it is matching the first [charts] to the very last [charts] and grabbing everything in the middle, because when I print it to the console I am seeing this:

[u'[chartsjs]\r\nname: mychart\r\ntype: line\r\nlabels: fish, cat, dog\r\ndata: 4, 5, 6\r\ndata2:5, 7, 9\r\n[/chartsjs]\r\n\r\nthis is some text now fool\r\n\r\n[chartsjs]\r\nname: second\r\ntype: line\r\nlabels: 100, 500, 1000\r\ndata: 50, 100, 10000\r\ndata2: 100, 100, 100\r\n[/chartsjs]']

I would like the regex to return the first match, eliminate the arbitrary test and then find another arbitrary number of matches. Is there a way to do this?

Mohammad Yusuf

You have got just 1 problem in your regex.

.* will greedily match everything in its path. When it encounters the first closing [/charts] it will go further to check if there are any more [/charts] ahead. If found then it will proceed.

To make it stop at first [/charts] we need to make it lazy by putting a question mark. .*? This will keep matching everything and stops at first [/charts]

Take a look I tested it:

import re

a="""
[charts]
name: mychart
type: line
labels: fish, cat, dog
data: 4, 5, 6
data2:5, 7, 9
[/charts]

this is some text

[charts]
name: second
type: line
labels: 100, 500, 1000
data: 50, 100, 10000
data2: 100, 100, 100
[/charts]
"""

for c in re.findall('(\[charts\].*?\[/charts\])',a, re.DOTALL):
    print c

Output:

[charts]
name: mychart
type: line
labels: fish, cat, dog
data: 4, 5, 6
data2:5, 7, 9
[/charts]
[charts]
name: second
type: line
labels: 100, 500, 1000
data: 50, 100, 10000
data2: 100, 100, 100
[/charts]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to stop swipe at some point in ItemTouchHelpers?

From Dev

how to use dotall flag for regex.exec()

From Dev

how to use dotall flag for regex.exec()

From Dev

How to make regex stop at a certain character

From Dev

How to make regex stop at a certain character

From Dev

Android (Eclispe): How to stop execution at some point in the middle of a program?

From Dev

How to make regex pattern lazy, or how to stop matching on first occurrence?

From Dev

How to make a fixed positioned div until some point?

From Dev

How to make a branch and revert the main branch to some point?

From Dev

How to make my regex match stop after a lookahead?

From Dev

How to match anything (DOTALL) without DOTALL?

From Dev

Animate TextView to increase integer and stop at some point?

From Dev

Non greedy dotall regex in Python

From Dev

How to stop NSTimer at a specific point?

From Dev

how to stop at a specific point in a list

From Dev

How do you match multiple lines with dot (DOTALL) in eclipse find regex

From Dev

How to make Loop stop

From Dev

How to make left and right divs sticky to the top on scroll at some point using JavaScript or jQuery?

From Dev

How can I make my TinyMCE regex stop stripping out semicolons?

From Dev

REGEX to make string start from specific point

From Dev

How to stop regex at the end of line

From Dev

Regex to match a sentence end with full stop(.) ignore decimal point

From Dev

Random .HTML, how to make it stop

From Dev

How to stop an object from moving past a point

From Dev

How to stop the movement of seekBar past a dynamic point

From Dev

How to make an if statement point free

From Dev

how to make a regex shorter

From Dev

How to stop recursion after some time in C

From Dev

How to stop multiple calls on some events in Jquery?

Related Related

  1. 1

    How to stop swipe at some point in ItemTouchHelpers?

  2. 2

    how to use dotall flag for regex.exec()

  3. 3

    how to use dotall flag for regex.exec()

  4. 4

    How to make regex stop at a certain character

  5. 5

    How to make regex stop at a certain character

  6. 6

    Android (Eclispe): How to stop execution at some point in the middle of a program?

  7. 7

    How to make regex pattern lazy, or how to stop matching on first occurrence?

  8. 8

    How to make a fixed positioned div until some point?

  9. 9

    How to make a branch and revert the main branch to some point?

  10. 10

    How to make my regex match stop after a lookahead?

  11. 11

    How to match anything (DOTALL) without DOTALL?

  12. 12

    Animate TextView to increase integer and stop at some point?

  13. 13

    Non greedy dotall regex in Python

  14. 14

    How to stop NSTimer at a specific point?

  15. 15

    how to stop at a specific point in a list

  16. 16

    How do you match multiple lines with dot (DOTALL) in eclipse find regex

  17. 17

    How to make Loop stop

  18. 18

    How to make left and right divs sticky to the top on scroll at some point using JavaScript or jQuery?

  19. 19

    How can I make my TinyMCE regex stop stripping out semicolons?

  20. 20

    REGEX to make string start from specific point

  21. 21

    How to stop regex at the end of line

  22. 22

    Regex to match a sentence end with full stop(.) ignore decimal point

  23. 23

    Random .HTML, how to make it stop

  24. 24

    How to stop an object from moving past a point

  25. 25

    How to stop the movement of seekBar past a dynamic point

  26. 26

    How to make an if statement point free

  27. 27

    how to make a regex shorter

  28. 28

    How to stop recursion after some time in C

  29. 29

    How to stop multiple calls on some events in Jquery?

HotTag

Archive