How to reconstruct regex matched part
I have simplify some latex math formula within text, for example
This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal
I want to transform this into
This is BaFe2As2 crystal
That is to concatenate only content within inner most bracket.
I figure out that I can use regex pattern
{[^{}]*}
to match those inner most bracket. But the problem is how to concatenate them together?
I don't know if this could be done in notepad++ regex replacement. If notepad++ is not capable, I can also accept perl one-liner solution.
regex perl
add a comment |
I have simplify some latex math formula within text, for example
This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal
I want to transform this into
This is BaFe2As2 crystal
That is to concatenate only content within inner most bracket.
I figure out that I can use regex pattern
{[^{}]*}
to match those inner most bracket. But the problem is how to concatenate them together?
I don't know if this could be done in notepad++ regex replacement. If notepad++ is not capable, I can also accept perl one-liner solution.
regex perl
I assume that you have more than one of these in the document. Are they all inline equations (between single$
s), like the one shown, or are there also displayed ones?
– zdim
Dec 29 '18 at 7:11
@zdim They are all inlines
– user15964
Dec 29 '18 at 8:07
Perhaps if you can use Sublime you might try(?:$(?=[^$]+$)|G(?!A)){(?>{([^{}]+)}|[^{}]+|(?R))*}_?$?
and replace with the first capturing group$1
demo
– The fourth bird
Dec 29 '18 at 16:12
add a comment |
I have simplify some latex math formula within text, for example
This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal
I want to transform this into
This is BaFe2As2 crystal
That is to concatenate only content within inner most bracket.
I figure out that I can use regex pattern
{[^{}]*}
to match those inner most bracket. But the problem is how to concatenate them together?
I don't know if this could be done in notepad++ regex replacement. If notepad++ is not capable, I can also accept perl one-liner solution.
regex perl
I have simplify some latex math formula within text, for example
This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal
I want to transform this into
This is BaFe2As2 crystal
That is to concatenate only content within inner most bracket.
I figure out that I can use regex pattern
{[^{}]*}
to match those inner most bracket. But the problem is how to concatenate them together?
I don't know if this could be done in notepad++ regex replacement. If notepad++ is not capable, I can also accept perl one-liner solution.
regex perl
regex perl
edited Dec 29 '18 at 8:07
user15964
asked Dec 29 '18 at 6:03
user15964user15964
763823
763823
I assume that you have more than one of these in the document. Are they all inline equations (between single$
s), like the one shown, or are there also displayed ones?
– zdim
Dec 29 '18 at 7:11
@zdim They are all inlines
– user15964
Dec 29 '18 at 8:07
Perhaps if you can use Sublime you might try(?:$(?=[^$]+$)|G(?!A)){(?>{([^{}]+)}|[^{}]+|(?R))*}_?$?
and replace with the first capturing group$1
demo
– The fourth bird
Dec 29 '18 at 16:12
add a comment |
I assume that you have more than one of these in the document. Are they all inline equations (between single$
s), like the one shown, or are there also displayed ones?
– zdim
Dec 29 '18 at 7:11
@zdim They are all inlines
– user15964
Dec 29 '18 at 8:07
Perhaps if you can use Sublime you might try(?:$(?=[^$]+$)|G(?!A)){(?>{([^{}]+)}|[^{}]+|(?R))*}_?$?
and replace with the first capturing group$1
demo
– The fourth bird
Dec 29 '18 at 16:12
I assume that you have more than one of these in the document. Are they all inline equations (between single
$
s), like the one shown, or are there also displayed ones?– zdim
Dec 29 '18 at 7:11
I assume that you have more than one of these in the document. Are they all inline equations (between single
$
s), like the one shown, or are there also displayed ones?– zdim
Dec 29 '18 at 7:11
@zdim They are all inlines
– user15964
Dec 29 '18 at 8:07
@zdim They are all inlines
– user15964
Dec 29 '18 at 8:07
Perhaps if you can use Sublime you might try
(?:$(?=[^$]+$)|G(?!A)){(?>{([^{}]+)}|[^{}]+|(?R))*}_?$?
and replace with the first capturing group $1
demo– The fourth bird
Dec 29 '18 at 16:12
Perhaps if you can use Sublime you might try
(?:$(?=[^$]+$)|G(?!A)){(?>{([^{}]+)}|[^{}]+|(?R))*}_?$?
and replace with the first capturing group $1
demo– The fourth bird
Dec 29 '18 at 16:12
add a comment |
2 Answers
2
active
oldest
votes
There may clearly be multiple such equations (the markup between two $
s) in the document. So while you need to assemble text between all {}
, this also need be constrained within a $
pair. Then all such equations need be processed.
Matching that in a single pattern results in a rather complex regex. Instead, we can first extract everything within a pair of $
s and then gather text within {}
s from that, simplifying regex a lot. This makes two passes over each equation but a Latex document is small for computational purposes and the loss of efficiency can't be noticed.
use warnings;
use strict;
use feature 'say';
my $text = q(This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal,)
. q( and ${text{Some}}{mathbf{More}}$ text);
my @results;
while ($text =~ /$(.*?)$/g) {
my $eq = $1;
push @results, join('', $eq =~ /{([^{}]+)}/g);
}
say for @results;
This prints lines BaFe2As2
and SomeMore
.
The regex in the while
condition captures all chars between two $
s. After the body of the loop executes and the condition is checked again, the regex continues searching the string from the position of the previous match. This is due to the "global" modifier /g
in scalar context, here imposed on regex by the loop condition. Once there are no more matches the loop terminates.
In the body we match between {}
, and again due to /g
this is done for all {}
s in the equation. Here, however, the regex is in the list context (as it is assigned to an array) and then /g
makes it return all matches. They are joined into a string, which is added to the array.
In order to replace the processed equation, use this in a substitution instead
$text =~ s{ $(.*?)$ }{ join('', $1 =~ /{([^{}]+)}/g) }egx;
where the modifier e
makes it so that the replacement part is evaluated as Perl code, and the result of that used to replace the matched part. Then in it we can run our regex to match content of all {}
and join it into the string, as explained above. I use s{}{}
delimiters, and x
modifier so to be able to space things in the matching part as well.
Since the whole substitution has the g
modifier the regex keeps going through $text
, as long as there are equations to match, replacing them with what's evaluated in the replacement part.
I use a hard-coded string (extended) from the question, for an easy demo. In reality you'd read a file into a scalar variable ("slurp" it) and process that.
This relies on the question's premise that text of interest in an equation is cleanly between {}
.
Missed the part that a one-liner is sought
perl -0777 -wnE'say join("", $1=~/{([^{}]+)}/g) while /$(.*?)$/g' file.tex
With -0777
the file is read whole ("slurped"), and as -n
provides a loop over input lines it is in the $_
variable; the regex in the while
condition works by default on $_
. In each interation of while
the contents of the captured equation, in $1
, is directly matched for {}
s.
Then to replace each equation and print out the whole processed file
perl -0777 -wne's{$(.*?)$}{join "", $1=~/{([^{}]+)}/g}eg; print' file.tex
where I've removed extra spaces and (unnecessary) parens on join
.
@user15964 I used your string, slightly extended, as an example. Let me know if it would be useful to show how to read a file into$text
instead. (Which is what you would actually do.)
– zdim
Dec 29 '18 at 9:51
@user15964 I had missed that you wanted specifically a one-liner. Added.
– zdim
Dec 31 '18 at 7:25
@user15964 Aparently I also missed that you want to have those equations replaced (even as you clearly state that!). Added that, to both the script version and the one-liner. Sorry about back and forths.
– zdim
Dec 31 '18 at 20:45
Thank you so much for your solution and thorough explanation ! Really learned a lot from your answer as a perl beginner : )
– user15964
Jan 1 at 3:01
@user15964 Really glad to hear that :) Let me know if something is missing or unclear, I'll add/edit.
– zdim
Jan 1 at 22:08
add a comment |
Use this regex in Notepad++. I have tried to match everything which is NOT present between the innermost curly brackets and then replaced the match with a blank string.
[^{}]*{|}[^{}]*
Click for Demo
Explanation:
[^{}]*{
- matches 0+ occurrences of any character that is neither{
nor}
followed by{
|
- OR
}[^{}]*
- matches}
followed by 0+ occurrences of any character that is neither{
nor}
Before Replacement:
After Replacement:
UPDATE:
Try this updated regex:
$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
Click for Demo
Quite great! upvoted. However, I found your method is opposite to my intention. In reality, I have to deal with latex fomula within text. So for example, change "This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal" into "This is BaFe2As2 crystal". This is my fault, I'll update my post.
– user15964
Dec 29 '18 at 8:05
@user15964 I have added an update that should do the job.
– Gurman
Dec 29 '18 at 9:32
Thank you so much! Have you tried the updated version in notepad++, it seems not compatible in notepad++ as I test
– user15964
Dec 29 '18 at 12:39
@user15964 After escaping the curly brackets, it is working in Notepad++ too -$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
– Gurman
Dec 29 '18 at 17:26
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',
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
});
}
});
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%2f53967131%2fhow-to-reconstruct-regex-matched-part%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There may clearly be multiple such equations (the markup between two $
s) in the document. So while you need to assemble text between all {}
, this also need be constrained within a $
pair. Then all such equations need be processed.
Matching that in a single pattern results in a rather complex regex. Instead, we can first extract everything within a pair of $
s and then gather text within {}
s from that, simplifying regex a lot. This makes two passes over each equation but a Latex document is small for computational purposes and the loss of efficiency can't be noticed.
use warnings;
use strict;
use feature 'say';
my $text = q(This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal,)
. q( and ${text{Some}}{mathbf{More}}$ text);
my @results;
while ($text =~ /$(.*?)$/g) {
my $eq = $1;
push @results, join('', $eq =~ /{([^{}]+)}/g);
}
say for @results;
This prints lines BaFe2As2
and SomeMore
.
The regex in the while
condition captures all chars between two $
s. After the body of the loop executes and the condition is checked again, the regex continues searching the string from the position of the previous match. This is due to the "global" modifier /g
in scalar context, here imposed on regex by the loop condition. Once there are no more matches the loop terminates.
In the body we match between {}
, and again due to /g
this is done for all {}
s in the equation. Here, however, the regex is in the list context (as it is assigned to an array) and then /g
makes it return all matches. They are joined into a string, which is added to the array.
In order to replace the processed equation, use this in a substitution instead
$text =~ s{ $(.*?)$ }{ join('', $1 =~ /{([^{}]+)}/g) }egx;
where the modifier e
makes it so that the replacement part is evaluated as Perl code, and the result of that used to replace the matched part. Then in it we can run our regex to match content of all {}
and join it into the string, as explained above. I use s{}{}
delimiters, and x
modifier so to be able to space things in the matching part as well.
Since the whole substitution has the g
modifier the regex keeps going through $text
, as long as there are equations to match, replacing them with what's evaluated in the replacement part.
I use a hard-coded string (extended) from the question, for an easy demo. In reality you'd read a file into a scalar variable ("slurp" it) and process that.
This relies on the question's premise that text of interest in an equation is cleanly between {}
.
Missed the part that a one-liner is sought
perl -0777 -wnE'say join("", $1=~/{([^{}]+)}/g) while /$(.*?)$/g' file.tex
With -0777
the file is read whole ("slurped"), and as -n
provides a loop over input lines it is in the $_
variable; the regex in the while
condition works by default on $_
. In each interation of while
the contents of the captured equation, in $1
, is directly matched for {}
s.
Then to replace each equation and print out the whole processed file
perl -0777 -wne's{$(.*?)$}{join "", $1=~/{([^{}]+)}/g}eg; print' file.tex
where I've removed extra spaces and (unnecessary) parens on join
.
@user15964 I used your string, slightly extended, as an example. Let me know if it would be useful to show how to read a file into$text
instead. (Which is what you would actually do.)
– zdim
Dec 29 '18 at 9:51
@user15964 I had missed that you wanted specifically a one-liner. Added.
– zdim
Dec 31 '18 at 7:25
@user15964 Aparently I also missed that you want to have those equations replaced (even as you clearly state that!). Added that, to both the script version and the one-liner. Sorry about back and forths.
– zdim
Dec 31 '18 at 20:45
Thank you so much for your solution and thorough explanation ! Really learned a lot from your answer as a perl beginner : )
– user15964
Jan 1 at 3:01
@user15964 Really glad to hear that :) Let me know if something is missing or unclear, I'll add/edit.
– zdim
Jan 1 at 22:08
add a comment |
There may clearly be multiple such equations (the markup between two $
s) in the document. So while you need to assemble text between all {}
, this also need be constrained within a $
pair. Then all such equations need be processed.
Matching that in a single pattern results in a rather complex regex. Instead, we can first extract everything within a pair of $
s and then gather text within {}
s from that, simplifying regex a lot. This makes two passes over each equation but a Latex document is small for computational purposes and the loss of efficiency can't be noticed.
use warnings;
use strict;
use feature 'say';
my $text = q(This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal,)
. q( and ${text{Some}}{mathbf{More}}$ text);
my @results;
while ($text =~ /$(.*?)$/g) {
my $eq = $1;
push @results, join('', $eq =~ /{([^{}]+)}/g);
}
say for @results;
This prints lines BaFe2As2
and SomeMore
.
The regex in the while
condition captures all chars between two $
s. After the body of the loop executes and the condition is checked again, the regex continues searching the string from the position of the previous match. This is due to the "global" modifier /g
in scalar context, here imposed on regex by the loop condition. Once there are no more matches the loop terminates.
In the body we match between {}
, and again due to /g
this is done for all {}
s in the equation. Here, however, the regex is in the list context (as it is assigned to an array) and then /g
makes it return all matches. They are joined into a string, which is added to the array.
In order to replace the processed equation, use this in a substitution instead
$text =~ s{ $(.*?)$ }{ join('', $1 =~ /{([^{}]+)}/g) }egx;
where the modifier e
makes it so that the replacement part is evaluated as Perl code, and the result of that used to replace the matched part. Then in it we can run our regex to match content of all {}
and join it into the string, as explained above. I use s{}{}
delimiters, and x
modifier so to be able to space things in the matching part as well.
Since the whole substitution has the g
modifier the regex keeps going through $text
, as long as there are equations to match, replacing them with what's evaluated in the replacement part.
I use a hard-coded string (extended) from the question, for an easy demo. In reality you'd read a file into a scalar variable ("slurp" it) and process that.
This relies on the question's premise that text of interest in an equation is cleanly between {}
.
Missed the part that a one-liner is sought
perl -0777 -wnE'say join("", $1=~/{([^{}]+)}/g) while /$(.*?)$/g' file.tex
With -0777
the file is read whole ("slurped"), and as -n
provides a loop over input lines it is in the $_
variable; the regex in the while
condition works by default on $_
. In each interation of while
the contents of the captured equation, in $1
, is directly matched for {}
s.
Then to replace each equation and print out the whole processed file
perl -0777 -wne's{$(.*?)$}{join "", $1=~/{([^{}]+)}/g}eg; print' file.tex
where I've removed extra spaces and (unnecessary) parens on join
.
@user15964 I used your string, slightly extended, as an example. Let me know if it would be useful to show how to read a file into$text
instead. (Which is what you would actually do.)
– zdim
Dec 29 '18 at 9:51
@user15964 I had missed that you wanted specifically a one-liner. Added.
– zdim
Dec 31 '18 at 7:25
@user15964 Aparently I also missed that you want to have those equations replaced (even as you clearly state that!). Added that, to both the script version and the one-liner. Sorry about back and forths.
– zdim
Dec 31 '18 at 20:45
Thank you so much for your solution and thorough explanation ! Really learned a lot from your answer as a perl beginner : )
– user15964
Jan 1 at 3:01
@user15964 Really glad to hear that :) Let me know if something is missing or unclear, I'll add/edit.
– zdim
Jan 1 at 22:08
add a comment |
There may clearly be multiple such equations (the markup between two $
s) in the document. So while you need to assemble text between all {}
, this also need be constrained within a $
pair. Then all such equations need be processed.
Matching that in a single pattern results in a rather complex regex. Instead, we can first extract everything within a pair of $
s and then gather text within {}
s from that, simplifying regex a lot. This makes two passes over each equation but a Latex document is small for computational purposes and the loss of efficiency can't be noticed.
use warnings;
use strict;
use feature 'say';
my $text = q(This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal,)
. q( and ${text{Some}}{mathbf{More}}$ text);
my @results;
while ($text =~ /$(.*?)$/g) {
my $eq = $1;
push @results, join('', $eq =~ /{([^{}]+)}/g);
}
say for @results;
This prints lines BaFe2As2
and SomeMore
.
The regex in the while
condition captures all chars between two $
s. After the body of the loop executes and the condition is checked again, the regex continues searching the string from the position of the previous match. This is due to the "global" modifier /g
in scalar context, here imposed on regex by the loop condition. Once there are no more matches the loop terminates.
In the body we match between {}
, and again due to /g
this is done for all {}
s in the equation. Here, however, the regex is in the list context (as it is assigned to an array) and then /g
makes it return all matches. They are joined into a string, which is added to the array.
In order to replace the processed equation, use this in a substitution instead
$text =~ s{ $(.*?)$ }{ join('', $1 =~ /{([^{}]+)}/g) }egx;
where the modifier e
makes it so that the replacement part is evaluated as Perl code, and the result of that used to replace the matched part. Then in it we can run our regex to match content of all {}
and join it into the string, as explained above. I use s{}{}
delimiters, and x
modifier so to be able to space things in the matching part as well.
Since the whole substitution has the g
modifier the regex keeps going through $text
, as long as there are equations to match, replacing them with what's evaluated in the replacement part.
I use a hard-coded string (extended) from the question, for an easy demo. In reality you'd read a file into a scalar variable ("slurp" it) and process that.
This relies on the question's premise that text of interest in an equation is cleanly between {}
.
Missed the part that a one-liner is sought
perl -0777 -wnE'say join("", $1=~/{([^{}]+)}/g) while /$(.*?)$/g' file.tex
With -0777
the file is read whole ("slurped"), and as -n
provides a loop over input lines it is in the $_
variable; the regex in the while
condition works by default on $_
. In each interation of while
the contents of the captured equation, in $1
, is directly matched for {}
s.
Then to replace each equation and print out the whole processed file
perl -0777 -wne's{$(.*?)$}{join "", $1=~/{([^{}]+)}/g}eg; print' file.tex
where I've removed extra spaces and (unnecessary) parens on join
.
There may clearly be multiple such equations (the markup between two $
s) in the document. So while you need to assemble text between all {}
, this also need be constrained within a $
pair. Then all such equations need be processed.
Matching that in a single pattern results in a rather complex regex. Instead, we can first extract everything within a pair of $
s and then gather text within {}
s from that, simplifying regex a lot. This makes two passes over each equation but a Latex document is small for computational purposes and the loss of efficiency can't be noticed.
use warnings;
use strict;
use feature 'say';
my $text = q(This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal,)
. q( and ${text{Some}}{mathbf{More}}$ text);
my @results;
while ($text =~ /$(.*?)$/g) {
my $eq = $1;
push @results, join('', $eq =~ /{([^{}]+)}/g);
}
say for @results;
This prints lines BaFe2As2
and SomeMore
.
The regex in the while
condition captures all chars between two $
s. After the body of the loop executes and the condition is checked again, the regex continues searching the string from the position of the previous match. This is due to the "global" modifier /g
in scalar context, here imposed on regex by the loop condition. Once there are no more matches the loop terminates.
In the body we match between {}
, and again due to /g
this is done for all {}
s in the equation. Here, however, the regex is in the list context (as it is assigned to an array) and then /g
makes it return all matches. They are joined into a string, which is added to the array.
In order to replace the processed equation, use this in a substitution instead
$text =~ s{ $(.*?)$ }{ join('', $1 =~ /{([^{}]+)}/g) }egx;
where the modifier e
makes it so that the replacement part is evaluated as Perl code, and the result of that used to replace the matched part. Then in it we can run our regex to match content of all {}
and join it into the string, as explained above. I use s{}{}
delimiters, and x
modifier so to be able to space things in the matching part as well.
Since the whole substitution has the g
modifier the regex keeps going through $text
, as long as there are equations to match, replacing them with what's evaluated in the replacement part.
I use a hard-coded string (extended) from the question, for an easy demo. In reality you'd read a file into a scalar variable ("slurp" it) and process that.
This relies on the question's premise that text of interest in an equation is cleanly between {}
.
Missed the part that a one-liner is sought
perl -0777 -wnE'say join("", $1=~/{([^{}]+)}/g) while /$(.*?)$/g' file.tex
With -0777
the file is read whole ("slurped"), and as -n
provides a loop over input lines it is in the $_
variable; the regex in the while
condition works by default on $_
. In each interation of while
the contents of the captured equation, in $1
, is directly matched for {}
s.
Then to replace each equation and print out the whole processed file
perl -0777 -wne's{$(.*?)$}{join "", $1=~/{([^{}]+)}/g}eg; print' file.tex
where I've removed extra spaces and (unnecessary) parens on join
.
edited Jan 1 at 21:59
answered Dec 29 '18 at 9:06
zdimzdim
32.3k32041
32.3k32041
@user15964 I used your string, slightly extended, as an example. Let me know if it would be useful to show how to read a file into$text
instead. (Which is what you would actually do.)
– zdim
Dec 29 '18 at 9:51
@user15964 I had missed that you wanted specifically a one-liner. Added.
– zdim
Dec 31 '18 at 7:25
@user15964 Aparently I also missed that you want to have those equations replaced (even as you clearly state that!). Added that, to both the script version and the one-liner. Sorry about back and forths.
– zdim
Dec 31 '18 at 20:45
Thank you so much for your solution and thorough explanation ! Really learned a lot from your answer as a perl beginner : )
– user15964
Jan 1 at 3:01
@user15964 Really glad to hear that :) Let me know if something is missing or unclear, I'll add/edit.
– zdim
Jan 1 at 22:08
add a comment |
@user15964 I used your string, slightly extended, as an example. Let me know if it would be useful to show how to read a file into$text
instead. (Which is what you would actually do.)
– zdim
Dec 29 '18 at 9:51
@user15964 I had missed that you wanted specifically a one-liner. Added.
– zdim
Dec 31 '18 at 7:25
@user15964 Aparently I also missed that you want to have those equations replaced (even as you clearly state that!). Added that, to both the script version and the one-liner. Sorry about back and forths.
– zdim
Dec 31 '18 at 20:45
Thank you so much for your solution and thorough explanation ! Really learned a lot from your answer as a perl beginner : )
– user15964
Jan 1 at 3:01
@user15964 Really glad to hear that :) Let me know if something is missing or unclear, I'll add/edit.
– zdim
Jan 1 at 22:08
@user15964 I used your string, slightly extended, as an example. Let me know if it would be useful to show how to read a file into
$text
instead. (Which is what you would actually do.)– zdim
Dec 29 '18 at 9:51
@user15964 I used your string, slightly extended, as an example. Let me know if it would be useful to show how to read a file into
$text
instead. (Which is what you would actually do.)– zdim
Dec 29 '18 at 9:51
@user15964 I had missed that you wanted specifically a one-liner. Added.
– zdim
Dec 31 '18 at 7:25
@user15964 I had missed that you wanted specifically a one-liner. Added.
– zdim
Dec 31 '18 at 7:25
@user15964 Aparently I also missed that you want to have those equations replaced (even as you clearly state that!). Added that, to both the script version and the one-liner. Sorry about back and forths.
– zdim
Dec 31 '18 at 20:45
@user15964 Aparently I also missed that you want to have those equations replaced (even as you clearly state that!). Added that, to both the script version and the one-liner. Sorry about back and forths.
– zdim
Dec 31 '18 at 20:45
Thank you so much for your solution and thorough explanation ! Really learned a lot from your answer as a perl beginner : )
– user15964
Jan 1 at 3:01
Thank you so much for your solution and thorough explanation ! Really learned a lot from your answer as a perl beginner : )
– user15964
Jan 1 at 3:01
@user15964 Really glad to hear that :) Let me know if something is missing or unclear, I'll add/edit.
– zdim
Jan 1 at 22:08
@user15964 Really glad to hear that :) Let me know if something is missing or unclear, I'll add/edit.
– zdim
Jan 1 at 22:08
add a comment |
Use this regex in Notepad++. I have tried to match everything which is NOT present between the innermost curly brackets and then replaced the match with a blank string.
[^{}]*{|}[^{}]*
Click for Demo
Explanation:
[^{}]*{
- matches 0+ occurrences of any character that is neither{
nor}
followed by{
|
- OR
}[^{}]*
- matches}
followed by 0+ occurrences of any character that is neither{
nor}
Before Replacement:
After Replacement:
UPDATE:
Try this updated regex:
$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
Click for Demo
Quite great! upvoted. However, I found your method is opposite to my intention. In reality, I have to deal with latex fomula within text. So for example, change "This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal" into "This is BaFe2As2 crystal". This is my fault, I'll update my post.
– user15964
Dec 29 '18 at 8:05
@user15964 I have added an update that should do the job.
– Gurman
Dec 29 '18 at 9:32
Thank you so much! Have you tried the updated version in notepad++, it seems not compatible in notepad++ as I test
– user15964
Dec 29 '18 at 12:39
@user15964 After escaping the curly brackets, it is working in Notepad++ too -$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
– Gurman
Dec 29 '18 at 17:26
add a comment |
Use this regex in Notepad++. I have tried to match everything which is NOT present between the innermost curly brackets and then replaced the match with a blank string.
[^{}]*{|}[^{}]*
Click for Demo
Explanation:
[^{}]*{
- matches 0+ occurrences of any character that is neither{
nor}
followed by{
|
- OR
}[^{}]*
- matches}
followed by 0+ occurrences of any character that is neither{
nor}
Before Replacement:
After Replacement:
UPDATE:
Try this updated regex:
$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
Click for Demo
Quite great! upvoted. However, I found your method is opposite to my intention. In reality, I have to deal with latex fomula within text. So for example, change "This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal" into "This is BaFe2As2 crystal". This is my fault, I'll update my post.
– user15964
Dec 29 '18 at 8:05
@user15964 I have added an update that should do the job.
– Gurman
Dec 29 '18 at 9:32
Thank you so much! Have you tried the updated version in notepad++, it seems not compatible in notepad++ as I test
– user15964
Dec 29 '18 at 12:39
@user15964 After escaping the curly brackets, it is working in Notepad++ too -$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
– Gurman
Dec 29 '18 at 17:26
add a comment |
Use this regex in Notepad++. I have tried to match everything which is NOT present between the innermost curly brackets and then replaced the match with a blank string.
[^{}]*{|}[^{}]*
Click for Demo
Explanation:
[^{}]*{
- matches 0+ occurrences of any character that is neither{
nor}
followed by{
|
- OR
}[^{}]*
- matches}
followed by 0+ occurrences of any character that is neither{
nor}
Before Replacement:
After Replacement:
UPDATE:
Try this updated regex:
$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
Click for Demo
Use this regex in Notepad++. I have tried to match everything which is NOT present between the innermost curly brackets and then replaced the match with a blank string.
[^{}]*{|}[^{}]*
Click for Demo
Explanation:
[^{}]*{
- matches 0+ occurrences of any character that is neither{
nor}
followed by{
|
- OR
}[^{}]*
- matches}
followed by 0+ occurrences of any character that is neither{
nor}
Before Replacement:
After Replacement:
UPDATE:
Try this updated regex:
$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
Click for Demo
edited Dec 29 '18 at 9:37
answered Dec 29 '18 at 7:02
GurmanGurman
6,6652931
6,6652931
Quite great! upvoted. However, I found your method is opposite to my intention. In reality, I have to deal with latex fomula within text. So for example, change "This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal" into "This is BaFe2As2 crystal". This is my fault, I'll update my post.
– user15964
Dec 29 '18 at 8:05
@user15964 I have added an update that should do the job.
– Gurman
Dec 29 '18 at 9:32
Thank you so much! Have you tried the updated version in notepad++, it seems not compatible in notepad++ as I test
– user15964
Dec 29 '18 at 12:39
@user15964 After escaping the curly brackets, it is working in Notepad++ too -$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
– Gurman
Dec 29 '18 at 17:26
add a comment |
Quite great! upvoted. However, I found your method is opposite to my intention. In reality, I have to deal with latex fomula within text. So for example, change "This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal" into "This is BaFe2As2 crystal". This is my fault, I'll update my post.
– user15964
Dec 29 '18 at 8:05
@user15964 I have added an update that should do the job.
– Gurman
Dec 29 '18 at 9:32
Thank you so much! Have you tried the updated version in notepad++, it seems not compatible in notepad++ as I test
– user15964
Dec 29 '18 at 12:39
@user15964 After escaping the curly brackets, it is working in Notepad++ too -$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
– Gurman
Dec 29 '18 at 17:26
Quite great! upvoted. However, I found your method is opposite to my intention. In reality, I have to deal with latex fomula within text. So for example, change "This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal" into "This is BaFe2As2 crystal". This is my fault, I'll update my post.
– user15964
Dec 29 '18 at 8:05
Quite great! upvoted. However, I found your method is opposite to my intention. In reality, I have to deal with latex fomula within text. So for example, change "This is ${text{BaFe}}_{2}{text{As}}_{2}$ crystal" into "This is BaFe2As2 crystal". This is my fault, I'll update my post.
– user15964
Dec 29 '18 at 8:05
@user15964 I have added an update that should do the job.
– Gurman
Dec 29 '18 at 9:32
@user15964 I have added an update that should do the job.
– Gurman
Dec 29 '18 at 9:32
Thank you so much! Have you tried the updated version in notepad++, it seems not compatible in notepad++ as I test
– user15964
Dec 29 '18 at 12:39
Thank you so much! Have you tried the updated version in notepad++, it seems not compatible in notepad++ as I test
– user15964
Dec 29 '18 at 12:39
@user15964 After escaping the curly brackets, it is working in Notepad++ too -
$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
– Gurman
Dec 29 '18 at 17:26
@user15964 After escaping the curly brackets, it is working in Notepad++ too -
$?(?=[^$]*$[^$]*$)(?:[^{}]*{|}[^{}]*)(?=[^$]*$[^$]*$)$?
– Gurman
Dec 29 '18 at 17:26
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.
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%2f53967131%2fhow-to-reconstruct-regex-matched-part%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
I assume that you have more than one of these in the document. Are they all inline equations (between single
$
s), like the one shown, or are there also displayed ones?– zdim
Dec 29 '18 at 7:11
@zdim They are all inlines
– user15964
Dec 29 '18 at 8:07
Perhaps if you can use Sublime you might try
(?:$(?=[^$]+$)|G(?!A)){(?>{([^{}]+)}|[^{}]+|(?R))*}_?$?
and replace with the first capturing group$1
demo– The fourth bird
Dec 29 '18 at 16:12