match multiple line using regular expression and modify these matched lines using regexp and PHP
I want to match multiple lines based on regular expression. Suppose I have the following string:
#include<math.h>
#include<stdio.h>
int main()
I want to replace #include<math.h>
with <span class="header">#include<math.h> </span>
and #include<stdio.h>
with <span class="header">#include<stdio.h></span>
. The final filtered string will looks like:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
At first I am trying to match my target word to reformat as follows:
/#include<[^>]+>/m
But this only select first line and the following is working for multiple lines:
/#include<[^>]+>/gm
Now I am trying to prepare these matched lines as follows:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
code:
<?php
$input = '#include<math.h>
#include<stdio.h>
int main()';
$input = preg_replace('/(#include<[^>]+>)/gm','<span class="header">$0</span>',$input);
echo $input;
?>
This codes gives an error and came to know 'g' is not supported by php. This simple task kills my day. Any help will be appreciated.
php regex
add a comment |
I want to match multiple lines based on regular expression. Suppose I have the following string:
#include<math.h>
#include<stdio.h>
int main()
I want to replace #include<math.h>
with <span class="header">#include<math.h> </span>
and #include<stdio.h>
with <span class="header">#include<stdio.h></span>
. The final filtered string will looks like:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
At first I am trying to match my target word to reformat as follows:
/#include<[^>]+>/m
But this only select first line and the following is working for multiple lines:
/#include<[^>]+>/gm
Now I am trying to prepare these matched lines as follows:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
code:
<?php
$input = '#include<math.h>
#include<stdio.h>
int main()';
$input = preg_replace('/(#include<[^>]+>)/gm','<span class="header">$0</span>',$input);
echo $input;
?>
This codes gives an error and came to know 'g' is not supported by php. This simple task kills my day. Any help will be appreciated.
php regex
Usepreg_match_all
orpreg_replace_callback
instead of making it multiline.
– mario
Nov 26 '18 at 2:07
2
Possible duplicate of How to add additional tag to a specific word using regular expression and php. This question seems incredibly similar to your previous question, please explain how this one's different, and why you feel justified in asking a new question.
– Davіd
Nov 26 '18 at 2:39
add a comment |
I want to match multiple lines based on regular expression. Suppose I have the following string:
#include<math.h>
#include<stdio.h>
int main()
I want to replace #include<math.h>
with <span class="header">#include<math.h> </span>
and #include<stdio.h>
with <span class="header">#include<stdio.h></span>
. The final filtered string will looks like:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
At first I am trying to match my target word to reformat as follows:
/#include<[^>]+>/m
But this only select first line and the following is working for multiple lines:
/#include<[^>]+>/gm
Now I am trying to prepare these matched lines as follows:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
code:
<?php
$input = '#include<math.h>
#include<stdio.h>
int main()';
$input = preg_replace('/(#include<[^>]+>)/gm','<span class="header">$0</span>',$input);
echo $input;
?>
This codes gives an error and came to know 'g' is not supported by php. This simple task kills my day. Any help will be appreciated.
php regex
I want to match multiple lines based on regular expression. Suppose I have the following string:
#include<math.h>
#include<stdio.h>
int main()
I want to replace #include<math.h>
with <span class="header">#include<math.h> </span>
and #include<stdio.h>
with <span class="header">#include<stdio.h></span>
. The final filtered string will looks like:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
At first I am trying to match my target word to reformat as follows:
/#include<[^>]+>/m
But this only select first line and the following is working for multiple lines:
/#include<[^>]+>/gm
Now I am trying to prepare these matched lines as follows:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
code:
<?php
$input = '#include<math.h>
#include<stdio.h>
int main()';
$input = preg_replace('/(#include<[^>]+>)/gm','<span class="header">$0</span>',$input);
echo $input;
?>
This codes gives an error and came to know 'g' is not supported by php. This simple task kills my day. Any help will be appreciated.
php regex
php regex
edited Nov 26 '18 at 3:03
mickmackusa
23.4k103658
23.4k103658
asked Nov 26 '18 at 2:03
Abdus Sattar BhuiyanAbdus Sattar Bhuiyan
1,60821539
1,60821539
Usepreg_match_all
orpreg_replace_callback
instead of making it multiline.
– mario
Nov 26 '18 at 2:07
2
Possible duplicate of How to add additional tag to a specific word using regular expression and php. This question seems incredibly similar to your previous question, please explain how this one's different, and why you feel justified in asking a new question.
– Davіd
Nov 26 '18 at 2:39
add a comment |
Usepreg_match_all
orpreg_replace_callback
instead of making it multiline.
– mario
Nov 26 '18 at 2:07
2
Possible duplicate of How to add additional tag to a specific word using regular expression and php. This question seems incredibly similar to your previous question, please explain how this one's different, and why you feel justified in asking a new question.
– Davіd
Nov 26 '18 at 2:39
Use
preg_match_all
or preg_replace_callback
instead of making it multiline.– mario
Nov 26 '18 at 2:07
Use
preg_match_all
or preg_replace_callback
instead of making it multiline.– mario
Nov 26 '18 at 2:07
2
2
Possible duplicate of How to add additional tag to a specific word using regular expression and php. This question seems incredibly similar to your previous question, please explain how this one's different, and why you feel justified in asking a new question.
– Davіd
Nov 26 '18 at 2:39
Possible duplicate of How to add additional tag to a specific word using regular expression and php. This question seems incredibly similar to your previous question, please explain how this one's different, and why you feel justified in asking a new question.
– Davіd
Nov 26 '18 at 2:39
add a comment |
1 Answer
1
active
oldest
votes
Here's a PHP solution to your problem:
$str = <<<EOF
#include<math.h>
#include<stdio.h>
int main()
EOF;
$str = preg_replace('/(#include<[^>]+>)/', '<span class="header">$1</span>', $str);
echo $str;
This outputs:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
3v41.org demo
preg_replace
will match all occurrences, instead of just the first. However, if you only want to match occurences, instead of replace them, you'll need to use preg_match_all
instead of preg_match
, because PHP doesn't support the /g
flag and instead has the preg_match_all
function for this.
For RegExr, you need to add the global g
flag to capture multiple matches, instead of just the first.
Your final regex should look like this:
/#include<[^>]+>/gm
RegExr demo
1
The/g
flag only works in JavaScript.
– mario
Nov 26 '18 at 2:07
@David. Thanks for your time. How to make this two lines as: <span class="header">#include<math.h></span> <span class="header">#include<stdio.h></span> in php?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:12
@AbdusSattarBhuiyan You should probably update your question with specific details about the PHP solution you've tried, and why it's not working, because right now your question is about your regex not working in RegExr.
– Davіd
Nov 26 '18 at 2:15
@Davіd: Update done. Would you recheck please?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:39
1
@AbdusSattarBhuiyan Please see updated answer, you shouldn't put the/g
flag in the PHPpreg_replace
function. Also, PHP capture groups start from$1
, not$0
.
– Davіd
Nov 26 '18 at 2:40
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%2f53473897%2fmatch-multiple-line-using-regular-expression-and-modify-these-matched-lines-usin%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
Here's a PHP solution to your problem:
$str = <<<EOF
#include<math.h>
#include<stdio.h>
int main()
EOF;
$str = preg_replace('/(#include<[^>]+>)/', '<span class="header">$1</span>', $str);
echo $str;
This outputs:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
3v41.org demo
preg_replace
will match all occurrences, instead of just the first. However, if you only want to match occurences, instead of replace them, you'll need to use preg_match_all
instead of preg_match
, because PHP doesn't support the /g
flag and instead has the preg_match_all
function for this.
For RegExr, you need to add the global g
flag to capture multiple matches, instead of just the first.
Your final regex should look like this:
/#include<[^>]+>/gm
RegExr demo
1
The/g
flag only works in JavaScript.
– mario
Nov 26 '18 at 2:07
@David. Thanks for your time. How to make this two lines as: <span class="header">#include<math.h></span> <span class="header">#include<stdio.h></span> in php?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:12
@AbdusSattarBhuiyan You should probably update your question with specific details about the PHP solution you've tried, and why it's not working, because right now your question is about your regex not working in RegExr.
– Davіd
Nov 26 '18 at 2:15
@Davіd: Update done. Would you recheck please?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:39
1
@AbdusSattarBhuiyan Please see updated answer, you shouldn't put the/g
flag in the PHPpreg_replace
function. Also, PHP capture groups start from$1
, not$0
.
– Davіd
Nov 26 '18 at 2:40
add a comment |
Here's a PHP solution to your problem:
$str = <<<EOF
#include<math.h>
#include<stdio.h>
int main()
EOF;
$str = preg_replace('/(#include<[^>]+>)/', '<span class="header">$1</span>', $str);
echo $str;
This outputs:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
3v41.org demo
preg_replace
will match all occurrences, instead of just the first. However, if you only want to match occurences, instead of replace them, you'll need to use preg_match_all
instead of preg_match
, because PHP doesn't support the /g
flag and instead has the preg_match_all
function for this.
For RegExr, you need to add the global g
flag to capture multiple matches, instead of just the first.
Your final regex should look like this:
/#include<[^>]+>/gm
RegExr demo
1
The/g
flag only works in JavaScript.
– mario
Nov 26 '18 at 2:07
@David. Thanks for your time. How to make this two lines as: <span class="header">#include<math.h></span> <span class="header">#include<stdio.h></span> in php?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:12
@AbdusSattarBhuiyan You should probably update your question with specific details about the PHP solution you've tried, and why it's not working, because right now your question is about your regex not working in RegExr.
– Davіd
Nov 26 '18 at 2:15
@Davіd: Update done. Would you recheck please?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:39
1
@AbdusSattarBhuiyan Please see updated answer, you shouldn't put the/g
flag in the PHPpreg_replace
function. Also, PHP capture groups start from$1
, not$0
.
– Davіd
Nov 26 '18 at 2:40
add a comment |
Here's a PHP solution to your problem:
$str = <<<EOF
#include<math.h>
#include<stdio.h>
int main()
EOF;
$str = preg_replace('/(#include<[^>]+>)/', '<span class="header">$1</span>', $str);
echo $str;
This outputs:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
3v41.org demo
preg_replace
will match all occurrences, instead of just the first. However, if you only want to match occurences, instead of replace them, you'll need to use preg_match_all
instead of preg_match
, because PHP doesn't support the /g
flag and instead has the preg_match_all
function for this.
For RegExr, you need to add the global g
flag to capture multiple matches, instead of just the first.
Your final regex should look like this:
/#include<[^>]+>/gm
RegExr demo
Here's a PHP solution to your problem:
$str = <<<EOF
#include<math.h>
#include<stdio.h>
int main()
EOF;
$str = preg_replace('/(#include<[^>]+>)/', '<span class="header">$1</span>', $str);
echo $str;
This outputs:
<span class="header">#include<math.h></span>
<span class="header">#include<stdio.h></span>
int main()
3v41.org demo
preg_replace
will match all occurrences, instead of just the first. However, if you only want to match occurences, instead of replace them, you'll need to use preg_match_all
instead of preg_match
, because PHP doesn't support the /g
flag and instead has the preg_match_all
function for this.
For RegExr, you need to add the global g
flag to capture multiple matches, instead of just the first.
Your final regex should look like this:
/#include<[^>]+>/gm
RegExr demo
edited Nov 26 '18 at 3:16
answered Nov 26 '18 at 2:04
DavіdDavіd
3,66841836
3,66841836
1
The/g
flag only works in JavaScript.
– mario
Nov 26 '18 at 2:07
@David. Thanks for your time. How to make this two lines as: <span class="header">#include<math.h></span> <span class="header">#include<stdio.h></span> in php?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:12
@AbdusSattarBhuiyan You should probably update your question with specific details about the PHP solution you've tried, and why it's not working, because right now your question is about your regex not working in RegExr.
– Davіd
Nov 26 '18 at 2:15
@Davіd: Update done. Would you recheck please?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:39
1
@AbdusSattarBhuiyan Please see updated answer, you shouldn't put the/g
flag in the PHPpreg_replace
function. Also, PHP capture groups start from$1
, not$0
.
– Davіd
Nov 26 '18 at 2:40
add a comment |
1
The/g
flag only works in JavaScript.
– mario
Nov 26 '18 at 2:07
@David. Thanks for your time. How to make this two lines as: <span class="header">#include<math.h></span> <span class="header">#include<stdio.h></span> in php?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:12
@AbdusSattarBhuiyan You should probably update your question with specific details about the PHP solution you've tried, and why it's not working, because right now your question is about your regex not working in RegExr.
– Davіd
Nov 26 '18 at 2:15
@Davіd: Update done. Would you recheck please?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:39
1
@AbdusSattarBhuiyan Please see updated answer, you shouldn't put the/g
flag in the PHPpreg_replace
function. Also, PHP capture groups start from$1
, not$0
.
– Davіd
Nov 26 '18 at 2:40
1
1
The
/g
flag only works in JavaScript.– mario
Nov 26 '18 at 2:07
The
/g
flag only works in JavaScript.– mario
Nov 26 '18 at 2:07
@David. Thanks for your time. How to make this two lines as: <span class="header">#include<math.h></span> <span class="header">#include<stdio.h></span> in php?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:12
@David. Thanks for your time. How to make this two lines as: <span class="header">#include<math.h></span> <span class="header">#include<stdio.h></span> in php?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:12
@AbdusSattarBhuiyan You should probably update your question with specific details about the PHP solution you've tried, and why it's not working, because right now your question is about your regex not working in RegExr.
– Davіd
Nov 26 '18 at 2:15
@AbdusSattarBhuiyan You should probably update your question with specific details about the PHP solution you've tried, and why it's not working, because right now your question is about your regex not working in RegExr.
– Davіd
Nov 26 '18 at 2:15
@Davіd: Update done. Would you recheck please?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:39
@Davіd: Update done. Would you recheck please?
– Abdus Sattar Bhuiyan
Nov 26 '18 at 2:39
1
1
@AbdusSattarBhuiyan Please see updated answer, you shouldn't put the
/g
flag in the PHP preg_replace
function. Also, PHP capture groups start from $1
, not $0
.– Davіd
Nov 26 '18 at 2:40
@AbdusSattarBhuiyan Please see updated answer, you shouldn't put the
/g
flag in the PHP preg_replace
function. Also, PHP capture groups start from $1
, not $0
.– Davіd
Nov 26 '18 at 2:40
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%2f53473897%2fmatch-multiple-line-using-regular-expression-and-modify-these-matched-lines-usin%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
Use
preg_match_all
orpreg_replace_callback
instead of making it multiline.– mario
Nov 26 '18 at 2:07
2
Possible duplicate of How to add additional tag to a specific word using regular expression and php. This question seems incredibly similar to your previous question, please explain how this one's different, and why you feel justified in asking a new question.
– Davіd
Nov 26 '18 at 2:39