regex: match repeated (arbitrary times) pattern, but sort in separate groups










2















I'm trying to match (if possible, only) the values of coordinates contained in lines like:



function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 1.0000 0.290000 2.0000 1.56000 3.0000 5.47000 4.0000 17.3000 4.50000 31.2000 5.0000 52.6000


The first couple is matched as wished, that is to say in two different groups, by



(?<=bcouples:s)(S+)s+(S+)s+


Then,



 (?<=bcouples:s)((S+)s+(S+)s+)+


matches the whole line, but only splits the last two coordinates in separate groups.



Precision: the number of couples of coordinates is not known, so just adding several times



(S+)s+(S+)s+


in the end of the regex is not an option.



Thanks for your input!










share|improve this question
























  • What environment are you using this in?

    – CertainPerformance
    Nov 12 '18 at 9:13











  • I'm using Python, adding this in tags

    – Dionysis
    Nov 12 '18 at 10:50















2















I'm trying to match (if possible, only) the values of coordinates contained in lines like:



function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 1.0000 0.290000 2.0000 1.56000 3.0000 5.47000 4.0000 17.3000 4.50000 31.2000 5.0000 52.6000


The first couple is matched as wished, that is to say in two different groups, by



(?<=bcouples:s)(S+)s+(S+)s+


Then,



 (?<=bcouples:s)((S+)s+(S+)s+)+


matches the whole line, but only splits the last two coordinates in separate groups.



Precision: the number of couples of coordinates is not known, so just adding several times



(S+)s+(S+)s+


in the end of the regex is not an option.



Thanks for your input!










share|improve this question
























  • What environment are you using this in?

    – CertainPerformance
    Nov 12 '18 at 9:13











  • I'm using Python, adding this in tags

    – Dionysis
    Nov 12 '18 at 10:50













2












2








2








I'm trying to match (if possible, only) the values of coordinates contained in lines like:



function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 1.0000 0.290000 2.0000 1.56000 3.0000 5.47000 4.0000 17.3000 4.50000 31.2000 5.0000 52.6000


The first couple is matched as wished, that is to say in two different groups, by



(?<=bcouples:s)(S+)s+(S+)s+


Then,



 (?<=bcouples:s)((S+)s+(S+)s+)+


matches the whole line, but only splits the last two coordinates in separate groups.



Precision: the number of couples of coordinates is not known, so just adding several times



(S+)s+(S+)s+


in the end of the regex is not an option.



Thanks for your input!










share|improve this question
















I'm trying to match (if possible, only) the values of coordinates contained in lines like:



function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 1.0000 0.290000 2.0000 1.56000 3.0000 5.47000 4.0000 17.3000 4.50000 31.2000 5.0000 52.6000


The first couple is matched as wished, that is to say in two different groups, by



(?<=bcouples:s)(S+)s+(S+)s+


Then,



 (?<=bcouples:s)((S+)s+(S+)s+)+


matches the whole line, but only splits the last two coordinates in separate groups.



Precision: the number of couples of coordinates is not known, so just adding several times



(S+)s+(S+)s+


in the end of the regex is not an option.



Thanks for your input!







python regex regex-group






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 10:50







Dionysis

















asked Nov 12 '18 at 9:13









DionysisDionysis

164




164












  • What environment are you using this in?

    – CertainPerformance
    Nov 12 '18 at 9:13











  • I'm using Python, adding this in tags

    – Dionysis
    Nov 12 '18 at 10:50

















  • What environment are you using this in?

    – CertainPerformance
    Nov 12 '18 at 9:13











  • I'm using Python, adding this in tags

    – Dionysis
    Nov 12 '18 at 10:50
















What environment are you using this in?

– CertainPerformance
Nov 12 '18 at 9:13





What environment are you using this in?

– CertainPerformance
Nov 12 '18 at 9:13













I'm using Python, adding this in tags

– Dionysis
Nov 12 '18 at 10:50





I'm using Python, adding this in tags

– Dionysis
Nov 12 '18 at 10:50












1 Answer
1






active

oldest

votes


















1














Use findall():



re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",s)

([d.Ee+-]+)s+([d.Ee+-]+) --> two float numbers,
() each of grouped;
(?:s+ ... )+? --> +? there can be more couples, ? means non-greedy matching,
(?: the outer group is not interesting;


Edit:
You can select the appropriate line:



 if "couples:" in s:
coords= re.findall(...)


If your text contains more "couples", you can split it. In the following example, we can apply the regex for the 2nd or 3rd, or both part of the splitted string:



s="function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) couples: 0.1E+00 0.2E+00 9.00000 0.900000E-01"

ls=s.split("couples")
print(ls)
['function f is described by the (x,y) ',
': 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) ',
': 0.1E+00 0.2E+00 9.00000 0.900000E-01']

re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",ls[1])
[('0.000000E+00', '0.000000E+00'), ('5.00000', '0.500000E-01')]





share|improve this answer

























  • Thanks, this answers the question perfectly, but I have two more questions: 1/ What if I would like to end the search at a specific word occurence? For example, imagine that after the f function, there is a g function described exactly by the same way. How do I get the couples of coordinates separately and why adding 's*function' at the end of your regex doesn't work? 2/ Do we really need the 're.findall()'? It looks like 're.search()' would be enough...

    – Dionysis
    Nov 13 '18 at 13:38






  • 1





    1/ I have edited the code above. 2/ re.search() stops at the first matching, findall() iterates further.

    – kantal
    Nov 13 '18 at 15:17










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%2f53258970%2fregex-match-repeated-arbitrary-times-pattern-but-sort-in-separate-groups%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









1














Use findall():



re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",s)

([d.Ee+-]+)s+([d.Ee+-]+) --> two float numbers,
() each of grouped;
(?:s+ ... )+? --> +? there can be more couples, ? means non-greedy matching,
(?: the outer group is not interesting;


Edit:
You can select the appropriate line:



 if "couples:" in s:
coords= re.findall(...)


If your text contains more "couples", you can split it. In the following example, we can apply the regex for the 2nd or 3rd, or both part of the splitted string:



s="function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) couples: 0.1E+00 0.2E+00 9.00000 0.900000E-01"

ls=s.split("couples")
print(ls)
['function f is described by the (x,y) ',
': 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) ',
': 0.1E+00 0.2E+00 9.00000 0.900000E-01']

re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",ls[1])
[('0.000000E+00', '0.000000E+00'), ('5.00000', '0.500000E-01')]





share|improve this answer

























  • Thanks, this answers the question perfectly, but I have two more questions: 1/ What if I would like to end the search at a specific word occurence? For example, imagine that after the f function, there is a g function described exactly by the same way. How do I get the couples of coordinates separately and why adding 's*function' at the end of your regex doesn't work? 2/ Do we really need the 're.findall()'? It looks like 're.search()' would be enough...

    – Dionysis
    Nov 13 '18 at 13:38






  • 1





    1/ I have edited the code above. 2/ re.search() stops at the first matching, findall() iterates further.

    – kantal
    Nov 13 '18 at 15:17















1














Use findall():



re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",s)

([d.Ee+-]+)s+([d.Ee+-]+) --> two float numbers,
() each of grouped;
(?:s+ ... )+? --> +? there can be more couples, ? means non-greedy matching,
(?: the outer group is not interesting;


Edit:
You can select the appropriate line:



 if "couples:" in s:
coords= re.findall(...)


If your text contains more "couples", you can split it. In the following example, we can apply the regex for the 2nd or 3rd, or both part of the splitted string:



s="function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) couples: 0.1E+00 0.2E+00 9.00000 0.900000E-01"

ls=s.split("couples")
print(ls)
['function f is described by the (x,y) ',
': 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) ',
': 0.1E+00 0.2E+00 9.00000 0.900000E-01']

re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",ls[1])
[('0.000000E+00', '0.000000E+00'), ('5.00000', '0.500000E-01')]





share|improve this answer

























  • Thanks, this answers the question perfectly, but I have two more questions: 1/ What if I would like to end the search at a specific word occurence? For example, imagine that after the f function, there is a g function described exactly by the same way. How do I get the couples of coordinates separately and why adding 's*function' at the end of your regex doesn't work? 2/ Do we really need the 're.findall()'? It looks like 're.search()' would be enough...

    – Dionysis
    Nov 13 '18 at 13:38






  • 1





    1/ I have edited the code above. 2/ re.search() stops at the first matching, findall() iterates further.

    – kantal
    Nov 13 '18 at 15:17













1












1








1







Use findall():



re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",s)

([d.Ee+-]+)s+([d.Ee+-]+) --> two float numbers,
() each of grouped;
(?:s+ ... )+? --> +? there can be more couples, ? means non-greedy matching,
(?: the outer group is not interesting;


Edit:
You can select the appropriate line:



 if "couples:" in s:
coords= re.findall(...)


If your text contains more "couples", you can split it. In the following example, we can apply the regex for the 2nd or 3rd, or both part of the splitted string:



s="function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) couples: 0.1E+00 0.2E+00 9.00000 0.900000E-01"

ls=s.split("couples")
print(ls)
['function f is described by the (x,y) ',
': 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) ',
': 0.1E+00 0.2E+00 9.00000 0.900000E-01']

re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",ls[1])
[('0.000000E+00', '0.000000E+00'), ('5.00000', '0.500000E-01')]





share|improve this answer















Use findall():



re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",s)

([d.Ee+-]+)s+([d.Ee+-]+) --> two float numbers,
() each of grouped;
(?:s+ ... )+? --> +? there can be more couples, ? means non-greedy matching,
(?: the outer group is not interesting;


Edit:
You can select the appropriate line:



 if "couples:" in s:
coords= re.findall(...)


If your text contains more "couples", you can split it. In the following example, we can apply the regex for the 2nd or 3rd, or both part of the splitted string:



s="function f is described by the (x,y) couples: 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) couples: 0.1E+00 0.2E+00 9.00000 0.900000E-01"

ls=s.split("couples")
print(ls)
['function f is described by the (x,y) ',
': 0.000000E+00 0.000000E+00 5.00000 0.500000E-01 function g is described by the (x,y) ',
': 0.1E+00 0.2E+00 9.00000 0.900000E-01']

re.findall(r"(?:s+([d.Ee+-]+)s+([d.Ee+-]+))+?",ls[1])
[('0.000000E+00', '0.000000E+00'), ('5.00000', '0.500000E-01')]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 15:14

























answered Nov 12 '18 at 15:53









kantalkantal

63227




63227












  • Thanks, this answers the question perfectly, but I have two more questions: 1/ What if I would like to end the search at a specific word occurence? For example, imagine that after the f function, there is a g function described exactly by the same way. How do I get the couples of coordinates separately and why adding 's*function' at the end of your regex doesn't work? 2/ Do we really need the 're.findall()'? It looks like 're.search()' would be enough...

    – Dionysis
    Nov 13 '18 at 13:38






  • 1





    1/ I have edited the code above. 2/ re.search() stops at the first matching, findall() iterates further.

    – kantal
    Nov 13 '18 at 15:17

















  • Thanks, this answers the question perfectly, but I have two more questions: 1/ What if I would like to end the search at a specific word occurence? For example, imagine that after the f function, there is a g function described exactly by the same way. How do I get the couples of coordinates separately and why adding 's*function' at the end of your regex doesn't work? 2/ Do we really need the 're.findall()'? It looks like 're.search()' would be enough...

    – Dionysis
    Nov 13 '18 at 13:38






  • 1





    1/ I have edited the code above. 2/ re.search() stops at the first matching, findall() iterates further.

    – kantal
    Nov 13 '18 at 15:17
















Thanks, this answers the question perfectly, but I have two more questions: 1/ What if I would like to end the search at a specific word occurence? For example, imagine that after the f function, there is a g function described exactly by the same way. How do I get the couples of coordinates separately and why adding 's*function' at the end of your regex doesn't work? 2/ Do we really need the 're.findall()'? It looks like 're.search()' would be enough...

– Dionysis
Nov 13 '18 at 13:38





Thanks, this answers the question perfectly, but I have two more questions: 1/ What if I would like to end the search at a specific word occurence? For example, imagine that after the f function, there is a g function described exactly by the same way. How do I get the couples of coordinates separately and why adding 's*function' at the end of your regex doesn't work? 2/ Do we really need the 're.findall()'? It looks like 're.search()' would be enough...

– Dionysis
Nov 13 '18 at 13:38




1




1





1/ I have edited the code above. 2/ re.search() stops at the first matching, findall() iterates further.

– kantal
Nov 13 '18 at 15:17





1/ I have edited the code above. 2/ re.search() stops at the first matching, findall() iterates further.

– kantal
Nov 13 '18 at 15:17

















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%2f53258970%2fregex-match-repeated-arbitrary-times-pattern-but-sort-in-separate-groups%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







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus