regex: match repeated (arbitrary times) pattern, but sort in separate groups
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
What environment are you using this in?
– CertainPerformance
Nov 12 at 9:13
I'm using Python, adding this in tags
– Dionysis
Nov 12 at 10:50
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
python regex regex-group
edited Nov 12 at 10:50
asked Nov 12 at 9:13
Dionysis
164
164
What environment are you using this in?
– CertainPerformance
Nov 12 at 9:13
I'm using Python, adding this in tags
– Dionysis
Nov 12 at 10:50
add a comment |
What environment are you using this in?
– CertainPerformance
Nov 12 at 9:13
I'm using Python, adding this in tags
– Dionysis
Nov 12 at 10:50
What environment are you using this in?
– CertainPerformance
Nov 12 at 9:13
What environment are you using this in?
– CertainPerformance
Nov 12 at 9:13
I'm using Python, adding this in tags
– Dionysis
Nov 12 at 10:50
I'm using Python, adding this in tags
– Dionysis
Nov 12 at 10:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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')]
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 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 at 15:17
add a comment |
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',
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%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
up vote
1
down vote
accepted
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')]
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 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 at 15:17
add a comment |
up vote
1
down vote
accepted
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')]
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 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 at 15:17
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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')]
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')]
edited Nov 13 at 15:14
answered Nov 12 at 15:53
kantal
60717
60717
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 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 at 15:17
add a comment |
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 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 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 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 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 at 15:17
1/ I have edited the code above. 2/ re.search() stops at the first matching, findall() iterates further.
– kantal
Nov 13 at 15:17
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53258970%2fregex-match-repeated-arbitrary-times-pattern-but-sort-in-separate-groups%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
What environment are you using this in?
– CertainPerformance
Nov 12 at 9:13
I'm using Python, adding this in tags
– Dionysis
Nov 12 at 10:50