Regex force group order
up vote
0
down vote
favorite
I'm new in regex and I have a question.
Like in this example, https://regex101.com/r/Iak7cF/1/ how do I force
src="wow"
to be in group 1, and
title="toto"
to be in group 2?
I want to capture this kind of text in any order only if it contains:
class="formula"
Am I doing it right?
regex
add a comment |
up vote
0
down vote
favorite
I'm new in regex and I have a question.
Like in this example, https://regex101.com/r/Iak7cF/1/ how do I force
src="wow"
to be in group 1, and
title="toto"
to be in group 2?
I want to capture this kind of text in any order only if it contains:
class="formula"
Am I doing it right?
regex
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm new in regex and I have a question.
Like in this example, https://regex101.com/r/Iak7cF/1/ how do I force
src="wow"
to be in group 1, and
title="toto"
to be in group 2?
I want to capture this kind of text in any order only if it contains:
class="formula"
Am I doing it right?
regex
I'm new in regex and I have a question.
Like in this example, https://regex101.com/r/Iak7cF/1/ how do I force
src="wow"
to be in group 1, and
title="toto"
to be in group 2?
I want to capture this kind of text in any order only if it contains:
class="formula"
Am I doing it right?
regex
regex
edited Nov 10 at 16:38
Liam Strilchuk
12515
12515
asked Nov 10 at 16:33
Plomging
62
62
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
-1
down vote
accepted
You'd better use an HTML parser
But if you really want to use regex, you have to use named groups to achieve what you want.
<img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>
DEMO
No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
– Plomging
Nov 10 at 17:36
@Plomging: Yes, you're right, but it's clearer when you use the named groups.
– Toto
Nov 10 at 18:12
@downvoter, what's wrong with this answer?
– Toto
Nov 11 at 10:47
add a comment |
up vote
-1
down vote
Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:
Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.
PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:
So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
accepted
You'd better use an HTML parser
But if you really want to use regex, you have to use named groups to achieve what you want.
<img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>
DEMO
No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
– Plomging
Nov 10 at 17:36
@Plomging: Yes, you're right, but it's clearer when you use the named groups.
– Toto
Nov 10 at 18:12
@downvoter, what's wrong with this answer?
– Toto
Nov 11 at 10:47
add a comment |
up vote
-1
down vote
accepted
You'd better use an HTML parser
But if you really want to use regex, you have to use named groups to achieve what you want.
<img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>
DEMO
No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
– Plomging
Nov 10 at 17:36
@Plomging: Yes, you're right, but it's clearer when you use the named groups.
– Toto
Nov 10 at 18:12
@downvoter, what's wrong with this answer?
– Toto
Nov 11 at 10:47
add a comment |
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
You'd better use an HTML parser
But if you really want to use regex, you have to use named groups to achieve what you want.
<img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>
DEMO
You'd better use an HTML parser
But if you really want to use regex, you have to use named groups to achieve what you want.
<img(?=[^>]*class="formula")(?=.*(?<src>src=".*"))(?=.*(?<title>title=".*")).*>
DEMO
answered Nov 10 at 17:19
Toto
63.8k175697
63.8k175697
No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
– Plomging
Nov 10 at 17:36
@Plomging: Yes, you're right, but it's clearer when you use the named groups.
– Toto
Nov 10 at 18:12
@downvoter, what's wrong with this answer?
– Toto
Nov 11 at 10:47
add a comment |
No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
– Plomging
Nov 10 at 17:36
@Plomging: Yes, you're right, but it's clearer when you use the named groups.
– Toto
Nov 10 at 18:12
@downvoter, what's wrong with this answer?
– Toto
Nov 11 at 10:47
No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
– Plomging
Nov 10 at 17:36
No need to use named groups with this regex <img(?=[^>]*class="formula")(?=.*(src=".*"))(?=.*(title=".*")).*> src will be in group 1 and title in group 2
– Plomging
Nov 10 at 17:36
@Plomging: Yes, you're right, but it's clearer when you use the named groups.
– Toto
Nov 10 at 18:12
@Plomging: Yes, you're right, but it's clearer when you use the named groups.
– Toto
Nov 10 at 18:12
@downvoter, what's wrong with this answer?
– Toto
Nov 11 at 10:47
@downvoter, what's wrong with this answer?
– Toto
Nov 11 at 10:47
add a comment |
up vote
-1
down vote
Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:
Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.
PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:
So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!
add a comment |
up vote
-1
down vote
Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:
Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.
PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:
So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:
Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.
PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:
So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!
Regular expressions are very flexible and powerful, but in general, they are not the right tool for parsing XML, HTML, or XHTML. From WinBatch:
Regular Expressions are only good for parsing text that is tightly defined. Since Regular Expressions don't really understand the context of matches, they can be fooled in a big way if the structure of the text changes. In particular, Regular Expressions have difficulty with hierarchy.
PerlMonks has a detailed explanation of why regex is not a good solution for all but the most simple of casess. They summarize it like this:
So I hope it is clear: Please, don't try to parse arbitrary XML/HTML with regexes!
answered Nov 10 at 17:05
bitinerant
463
463
add a comment |
add a comment |
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%2f53241032%2fregex-force-group-order%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