How to to transform x^(…) to x^{…} even when brackets are inside? Pure Regex possible?
I need to change many posts and edit the content so that ^(...)
becomes x^{...}
.
What I have so far:
$regexpattern = "/^((.*?))/";
$replaceregex = "^{$1}";
$content_new = preg_replace($regexpattern, $replaceregex, $content);
which works.
However, I realized that if there is a round bracket inside the round wrapping brackets, it fails.
Example:
this should be transformed a^(x²) and this b^(-(x²))
becomes:
this should be transformed a^{x²} and this b^{-(x²})
Needed:
this should be transformed a^(x²) and this b^{-(x²)}
How can I prevent this and only replace the last bracket? Or do we need to use PHP and explode the content with ^(...) into Arrays and replace?
It could also be that there are multiple brackets inside (even if rare).
PS: After writing this question I found: https://stackoverflow.com/a/27052437/1066234 but there is no solution provided for this specific case.
php regex
add a comment |
I need to change many posts and edit the content so that ^(...)
becomes x^{...}
.
What I have so far:
$regexpattern = "/^((.*?))/";
$replaceregex = "^{$1}";
$content_new = preg_replace($regexpattern, $replaceregex, $content);
which works.
However, I realized that if there is a round bracket inside the round wrapping brackets, it fails.
Example:
this should be transformed a^(x²) and this b^(-(x²))
becomes:
this should be transformed a^{x²} and this b^{-(x²})
Needed:
this should be transformed a^(x²) and this b^{-(x²)}
How can I prevent this and only replace the last bracket? Or do we need to use PHP and explode the content with ^(...) into Arrays and replace?
It could also be that there are multiple brackets inside (even if rare).
PS: After writing this question I found: https://stackoverflow.com/a/27052437/1066234 but there is no solution provided for this specific case.
php regex
Regular expressions typically have problems with all kinds of nested structures.
– Some programmer dude
Nov 12 '18 at 8:15
What is expected output forthis should be transformed a^(x²) and this b^(-(x²))
?
– anubhava
Nov 12 '18 at 8:22
1
this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
– Nahuel Fouilleul
Nov 12 '18 at 8:25
add a comment |
I need to change many posts and edit the content so that ^(...)
becomes x^{...}
.
What I have so far:
$regexpattern = "/^((.*?))/";
$replaceregex = "^{$1}";
$content_new = preg_replace($regexpattern, $replaceregex, $content);
which works.
However, I realized that if there is a round bracket inside the round wrapping brackets, it fails.
Example:
this should be transformed a^(x²) and this b^(-(x²))
becomes:
this should be transformed a^{x²} and this b^{-(x²})
Needed:
this should be transformed a^(x²) and this b^{-(x²)}
How can I prevent this and only replace the last bracket? Or do we need to use PHP and explode the content with ^(...) into Arrays and replace?
It could also be that there are multiple brackets inside (even if rare).
PS: After writing this question I found: https://stackoverflow.com/a/27052437/1066234 but there is no solution provided for this specific case.
php regex
I need to change many posts and edit the content so that ^(...)
becomes x^{...}
.
What I have so far:
$regexpattern = "/^((.*?))/";
$replaceregex = "^{$1}";
$content_new = preg_replace($regexpattern, $replaceregex, $content);
which works.
However, I realized that if there is a round bracket inside the round wrapping brackets, it fails.
Example:
this should be transformed a^(x²) and this b^(-(x²))
becomes:
this should be transformed a^{x²} and this b^{-(x²})
Needed:
this should be transformed a^(x²) and this b^{-(x²)}
How can I prevent this and only replace the last bracket? Or do we need to use PHP and explode the content with ^(...) into Arrays and replace?
It could also be that there are multiple brackets inside (even if rare).
PS: After writing this question I found: https://stackoverflow.com/a/27052437/1066234 but there is no solution provided for this specific case.
php regex
php regex
edited Nov 12 '18 at 8:28
anubhava
520k46316390
520k46316390
asked Nov 12 '18 at 8:12
Kai Noack
6,189659115
6,189659115
Regular expressions typically have problems with all kinds of nested structures.
– Some programmer dude
Nov 12 '18 at 8:15
What is expected output forthis should be transformed a^(x²) and this b^(-(x²))
?
– anubhava
Nov 12 '18 at 8:22
1
this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
– Nahuel Fouilleul
Nov 12 '18 at 8:25
add a comment |
Regular expressions typically have problems with all kinds of nested structures.
– Some programmer dude
Nov 12 '18 at 8:15
What is expected output forthis should be transformed a^(x²) and this b^(-(x²))
?
– anubhava
Nov 12 '18 at 8:22
1
this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
– Nahuel Fouilleul
Nov 12 '18 at 8:25
Regular expressions typically have problems with all kinds of nested structures.
– Some programmer dude
Nov 12 '18 at 8:15
Regular expressions typically have problems with all kinds of nested structures.
– Some programmer dude
Nov 12 '18 at 8:15
What is expected output for
this should be transformed a^(x²) and this b^(-(x²))
?– anubhava
Nov 12 '18 at 8:22
What is expected output for
this should be transformed a^(x²) and this b^(-(x²))
?– anubhava
Nov 12 '18 at 8:22
1
1
this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
– Nahuel Fouilleul
Nov 12 '18 at 8:25
this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
– Nahuel Fouilleul
Nov 12 '18 at 8:25
add a comment |
2 Answers
2
active
oldest
votes
Such regex matches only outter brackets
^(((([^()]|(?1))*)))
and replace with
^{$2}
demo on regex101
demo on sandbox
OP wants to match^
before(
– anubhava
Nov 12 '18 at 8:38
1
@anubhava Thanks, updated
– splash58
Nov 12 '18 at 8:41
Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
– Kai Noack
Nov 12 '18 at 10:12
Glad to help! Good luck!
– splash58
Nov 12 '18 at 10:13
add a comment |
You can use this recursive regex in PHP with a negative lookahead:
$str = 'this should be transformed a^(x²) and this b^(-(x²))';
$re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
$repl = preg_replace($re, '^{$2}', $str);
//=> this should be transformed a^(x²) and this b^{-(x²)}
RegEx Demo
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%2f53258096%2fhow-to-to-transform-x-to-x-even-when-brackets-are-inside-pure-regex%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
Such regex matches only outter brackets
^(((([^()]|(?1))*)))
and replace with
^{$2}
demo on regex101
demo on sandbox
OP wants to match^
before(
– anubhava
Nov 12 '18 at 8:38
1
@anubhava Thanks, updated
– splash58
Nov 12 '18 at 8:41
Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
– Kai Noack
Nov 12 '18 at 10:12
Glad to help! Good luck!
– splash58
Nov 12 '18 at 10:13
add a comment |
Such regex matches only outter brackets
^(((([^()]|(?1))*)))
and replace with
^{$2}
demo on regex101
demo on sandbox
OP wants to match^
before(
– anubhava
Nov 12 '18 at 8:38
1
@anubhava Thanks, updated
– splash58
Nov 12 '18 at 8:41
Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
– Kai Noack
Nov 12 '18 at 10:12
Glad to help! Good luck!
– splash58
Nov 12 '18 at 10:13
add a comment |
Such regex matches only outter brackets
^(((([^()]|(?1))*)))
and replace with
^{$2}
demo on regex101
demo on sandbox
Such regex matches only outter brackets
^(((([^()]|(?1))*)))
and replace with
^{$2}
demo on regex101
demo on sandbox
edited Nov 12 '18 at 9:14
answered Nov 12 '18 at 8:36
splash58
21k21225
21k21225
OP wants to match^
before(
– anubhava
Nov 12 '18 at 8:38
1
@anubhava Thanks, updated
– splash58
Nov 12 '18 at 8:41
Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
– Kai Noack
Nov 12 '18 at 10:12
Glad to help! Good luck!
– splash58
Nov 12 '18 at 10:13
add a comment |
OP wants to match^
before(
– anubhava
Nov 12 '18 at 8:38
1
@anubhava Thanks, updated
– splash58
Nov 12 '18 at 8:41
Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
– Kai Noack
Nov 12 '18 at 10:12
Glad to help! Good luck!
– splash58
Nov 12 '18 at 10:13
OP wants to match
^
before (
– anubhava
Nov 12 '18 at 8:38
OP wants to match
^
before (
– anubhava
Nov 12 '18 at 8:38
1
1
@anubhava Thanks, updated
– splash58
Nov 12 '18 at 8:41
@anubhava Thanks, updated
– splash58
Nov 12 '18 at 8:41
Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
– Kai Noack
Nov 12 '18 at 10:12
Worked nicely in my test: regex101.com/r/tMlAzv/10 (one line is a non-match on purpose)
– Kai Noack
Nov 12 '18 at 10:12
Glad to help! Good luck!
– splash58
Nov 12 '18 at 10:13
Glad to help! Good luck!
– splash58
Nov 12 '18 at 10:13
add a comment |
You can use this recursive regex in PHP with a negative lookahead:
$str = 'this should be transformed a^(x²) and this b^(-(x²))';
$re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
$repl = preg_replace($re, '^{$2}', $str);
//=> this should be transformed a^(x²) and this b^{-(x²)}
RegEx Demo
add a comment |
You can use this recursive regex in PHP with a negative lookahead:
$str = 'this should be transformed a^(x²) and this b^(-(x²))';
$re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
$repl = preg_replace($re, '^{$2}', $str);
//=> this should be transformed a^(x²) and this b^{-(x²)}
RegEx Demo
add a comment |
You can use this recursive regex in PHP with a negative lookahead:
$str = 'this should be transformed a^(x²) and this b^(-(x²))';
$re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
$repl = preg_replace($re, '^{$2}', $str);
//=> this should be transformed a^(x²) and this b^{-(x²)}
RegEx Demo
You can use this recursive regex in PHP with a negative lookahead:
$str = 'this should be transformed a^(x²) and this b^(-(x²))';
$re = '/^ ( ( ( (?: [^()]* | (?-2) )* ) ) ) (?!.*^()/x';
$repl = preg_replace($re, '^{$2}', $str);
//=> this should be transformed a^(x²) and this b^{-(x²)}
RegEx Demo
answered Nov 12 '18 at 8:36
anubhava
520k46316390
520k46316390
add a comment |
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%2f53258096%2fhow-to-to-transform-x-to-x-even-when-brackets-are-inside-pure-regex%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
Regular expressions typically have problems with all kinds of nested structures.
– Some programmer dude
Nov 12 '18 at 8:15
What is expected output for
this should be transformed a^(x²) and this b^(-(x²))
?– anubhava
Nov 12 '18 at 8:22
1
this recursive expression regex101.com/r/Hlt37z/2 seems to work don't know if recursive expression are supported by preg_replace
– Nahuel Fouilleul
Nov 12 '18 at 8:25