Ignoring apostrophe while capturing contents in single quotes REGEX2019 Community Moderator ElectionWhat is the difference between single-quoted and double-quoted strings in PHP?Reference — What does this symbol mean in PHP?Regex - non-capturing condition in larger captureRefine regex to distinguish between captchure group quotesREGEX to capture sentences with quotesRegex Capture Groups in C#JS RegEx replacement of a non-captured group?Distinguish quotes ' and apostrophes while tokenizing with regexRegex to find pattern NOT enclosed in quotesRepeat Capture Regex
Getting the || sign while using Kurier
Are small insurances worth it?
What is Tony Stark injecting into himself in Iron Man 3?
What is better: yes / no radio, or simple checkbox?
Finitely many repeated replacements
MySQL importing CSV files really slow
I reported the illegal activity of my boss to his boss. My boss found out. Now I am being punished. What should I do?
Can the alpha, lambda values of a glmnet object output determine whether ridge or Lasso?
Shifting between bemols (flats) and diesis (sharps)in the key signature
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
Why is a very small peak with larger m/z not considered to be the molecular ion?
Are there historical instances of the capital of a colonising country being temporarily or permanently shifted to one of its colonies?
Why couldn't the separatists legally leave the Republic?
Power Strip for Europe
Expressing logarithmic equations without logs
Why does cron require MTA for logging?
From an axiomatic set theoric approach why can we take uncountable unions?
Virginia employer terminated employee and wants signing bonus returned
Has a sovereign Communist government ever run, and conceded loss, on a fair election?
Minimizing with differential evolution
How does Ehrenfest's theorem apply to the quantum harmonic oscillator?
Is a piano played in the same way as a harmonium?
Proving a statement about real numbers
What is the generally accepted pronunciation of “topoi”?
Ignoring apostrophe while capturing contents in single quotes REGEX
2019 Community Moderator ElectionWhat is the difference between single-quoted and double-quoted strings in PHP?Reference — What does this symbol mean in PHP?Regex - non-capturing condition in larger captureRefine regex to distinguish between captchure group quotesREGEX to capture sentences with quotesRegex Capture Groups in C#JS RegEx replacement of a non-captured group?Distinguish quotes ' and apostrophes while tokenizing with regexRegex to find pattern NOT enclosed in quotesRepeat Capture Regex
The issue for me here is to capture the content inside single quotes(like 'xyz').
But the apostrophe which is the same symbol as a single quote(') is coming in the way!
The regex I've written is : /(w'w)(*SKIP)(*F)|('[^']*')/
The example i have used is : Hello ma'am 'This is Prashanth's book.'
What needs to be captured is : 'This is Prashanth's book.'
.
But, what's capured is : 'This is Prashanth'
!
Here is the link of what i tried on online regex tester
Any help is greatly appreciated. Thank you!
php regex pcre
add a comment |
The issue for me here is to capture the content inside single quotes(like 'xyz').
But the apostrophe which is the same symbol as a single quote(') is coming in the way!
The regex I've written is : /(w'w)(*SKIP)(*F)|('[^']*')/
The example i have used is : Hello ma'am 'This is Prashanth's book.'
What needs to be captured is : 'This is Prashanth's book.'
.
But, what's capured is : 'This is Prashanth'
!
Here is the link of what i tried on online regex tester
Any help is greatly appreciated. Thank you!
php regex pcre
add a comment |
The issue for me here is to capture the content inside single quotes(like 'xyz').
But the apostrophe which is the same symbol as a single quote(') is coming in the way!
The regex I've written is : /(w'w)(*SKIP)(*F)|('[^']*')/
The example i have used is : Hello ma'am 'This is Prashanth's book.'
What needs to be captured is : 'This is Prashanth's book.'
.
But, what's capured is : 'This is Prashanth'
!
Here is the link of what i tried on online regex tester
Any help is greatly appreciated. Thank you!
php regex pcre
The issue for me here is to capture the content inside single quotes(like 'xyz').
But the apostrophe which is the same symbol as a single quote(') is coming in the way!
The regex I've written is : /(w'w)(*SKIP)(*F)|('[^']*')/
The example i have used is : Hello ma'am 'This is Prashanth's book.'
What needs to be captured is : 'This is Prashanth's book.'
.
But, what's capured is : 'This is Prashanth'
!
Here is the link of what i tried on online regex tester
Any help is greatly appreciated. Thank you!
php regex pcre
php regex pcre
asked Mar 7 at 5:18
Prashanth BennyPrashanth Benny
1,2061225
1,2061225
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can't use [^']
to capture a text that contains '
with in and in your example, This is Prashanth's book.
contains a '
character within the text. You need to modify your regex to use .*?
instead of [^']
and can write your regex as this,
(w'w)(*SKIP)(*F)|('.*?'B)
Demo with your updated regex
Also, you don't need to escape a single quote '
as that has no special meaning in regex.
From your example, it is not clear whether you want the captured match to contain '
around the match or not. In case you don't want '
to be captured in the match, you can use a lookarounds based regex and use this,
(?<=B').*?(?='B)
Explanation of regex:
(?<=B')
- This positive look behind ensures what gets captured in match is preceded by a single quote which is not preceded by a word character which is ensured byB
.*?
- Captures the text in non-greedy manner(?='B)
- Ensures the matched text is followed by a single quote andB
ensures it doesn't match a quote that is immediately followed by any word character. E.g. it won't match an ending quote like's
Demo
1
Thanks @Pushpesh! working like a charm... :)
– Prashanth Benny
Mar 7 at 6:24
1
Pleased to help :)
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:25
1
did you downvote the question? Someone did! I dont undestand why! :(
– Prashanth Benny
Mar 7 at 6:26
1
@PrashanthBenny: No I didn't downvote. Why would I answer a question if I downvoted. Also, getting downvotes on questions in regex tag is a normal trend. Anyway, take my upvote as your question is good and you showed what you tried with samples.
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:28
1
Thank you so very much for your support Pushpesh. This just saved my day from being screwed up with this issue... I had not actually looked at B as a possibility at all :D
– Prashanth Benny
Mar 7 at 9:58
add a comment |
For the string you have provided, you can use the regex:
B'K(?:(?!'B).)+
Click for Demo
Explanation:
B
- a non-word boundary'
- matches a'
K
- forget everything matched so far(?:(?!'B).)+
- matches 1+ occurrences of any character(except newline) which does not start with'
followed by a non-word boundary
1
Wow,K
. Didn't know about it for years... Thanks!
– 1234ru
Mar 7 at 12:23
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%2f55036563%2fignoring-apostrophe-while-capturing-contents-in-single-quotes-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
You can't use [^']
to capture a text that contains '
with in and in your example, This is Prashanth's book.
contains a '
character within the text. You need to modify your regex to use .*?
instead of [^']
and can write your regex as this,
(w'w)(*SKIP)(*F)|('.*?'B)
Demo with your updated regex
Also, you don't need to escape a single quote '
as that has no special meaning in regex.
From your example, it is not clear whether you want the captured match to contain '
around the match or not. In case you don't want '
to be captured in the match, you can use a lookarounds based regex and use this,
(?<=B').*?(?='B)
Explanation of regex:
(?<=B')
- This positive look behind ensures what gets captured in match is preceded by a single quote which is not preceded by a word character which is ensured byB
.*?
- Captures the text in non-greedy manner(?='B)
- Ensures the matched text is followed by a single quote andB
ensures it doesn't match a quote that is immediately followed by any word character. E.g. it won't match an ending quote like's
Demo
1
Thanks @Pushpesh! working like a charm... :)
– Prashanth Benny
Mar 7 at 6:24
1
Pleased to help :)
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:25
1
did you downvote the question? Someone did! I dont undestand why! :(
– Prashanth Benny
Mar 7 at 6:26
1
@PrashanthBenny: No I didn't downvote. Why would I answer a question if I downvoted. Also, getting downvotes on questions in regex tag is a normal trend. Anyway, take my upvote as your question is good and you showed what you tried with samples.
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:28
1
Thank you so very much for your support Pushpesh. This just saved my day from being screwed up with this issue... I had not actually looked at B as a possibility at all :D
– Prashanth Benny
Mar 7 at 9:58
add a comment |
You can't use [^']
to capture a text that contains '
with in and in your example, This is Prashanth's book.
contains a '
character within the text. You need to modify your regex to use .*?
instead of [^']
and can write your regex as this,
(w'w)(*SKIP)(*F)|('.*?'B)
Demo with your updated regex
Also, you don't need to escape a single quote '
as that has no special meaning in regex.
From your example, it is not clear whether you want the captured match to contain '
around the match or not. In case you don't want '
to be captured in the match, you can use a lookarounds based regex and use this,
(?<=B').*?(?='B)
Explanation of regex:
(?<=B')
- This positive look behind ensures what gets captured in match is preceded by a single quote which is not preceded by a word character which is ensured byB
.*?
- Captures the text in non-greedy manner(?='B)
- Ensures the matched text is followed by a single quote andB
ensures it doesn't match a quote that is immediately followed by any word character. E.g. it won't match an ending quote like's
Demo
1
Thanks @Pushpesh! working like a charm... :)
– Prashanth Benny
Mar 7 at 6:24
1
Pleased to help :)
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:25
1
did you downvote the question? Someone did! I dont undestand why! :(
– Prashanth Benny
Mar 7 at 6:26
1
@PrashanthBenny: No I didn't downvote. Why would I answer a question if I downvoted. Also, getting downvotes on questions in regex tag is a normal trend. Anyway, take my upvote as your question is good and you showed what you tried with samples.
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:28
1
Thank you so very much for your support Pushpesh. This just saved my day from being screwed up with this issue... I had not actually looked at B as a possibility at all :D
– Prashanth Benny
Mar 7 at 9:58
add a comment |
You can't use [^']
to capture a text that contains '
with in and in your example, This is Prashanth's book.
contains a '
character within the text. You need to modify your regex to use .*?
instead of [^']
and can write your regex as this,
(w'w)(*SKIP)(*F)|('.*?'B)
Demo with your updated regex
Also, you don't need to escape a single quote '
as that has no special meaning in regex.
From your example, it is not clear whether you want the captured match to contain '
around the match or not. In case you don't want '
to be captured in the match, you can use a lookarounds based regex and use this,
(?<=B').*?(?='B)
Explanation of regex:
(?<=B')
- This positive look behind ensures what gets captured in match is preceded by a single quote which is not preceded by a word character which is ensured byB
.*?
- Captures the text in non-greedy manner(?='B)
- Ensures the matched text is followed by a single quote andB
ensures it doesn't match a quote that is immediately followed by any word character. E.g. it won't match an ending quote like's
Demo
You can't use [^']
to capture a text that contains '
with in and in your example, This is Prashanth's book.
contains a '
character within the text. You need to modify your regex to use .*?
instead of [^']
and can write your regex as this,
(w'w)(*SKIP)(*F)|('.*?'B)
Demo with your updated regex
Also, you don't need to escape a single quote '
as that has no special meaning in regex.
From your example, it is not clear whether you want the captured match to contain '
around the match or not. In case you don't want '
to be captured in the match, you can use a lookarounds based regex and use this,
(?<=B').*?(?='B)
Explanation of regex:
(?<=B')
- This positive look behind ensures what gets captured in match is preceded by a single quote which is not preceded by a word character which is ensured byB
.*?
- Captures the text in non-greedy manner(?='B)
- Ensures the matched text is followed by a single quote andB
ensures it doesn't match a quote that is immediately followed by any word character. E.g. it won't match an ending quote like's
Demo
edited Mar 7 at 5:36
answered Mar 7 at 5:24
Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi
9,28321029
9,28321029
1
Thanks @Pushpesh! working like a charm... :)
– Prashanth Benny
Mar 7 at 6:24
1
Pleased to help :)
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:25
1
did you downvote the question? Someone did! I dont undestand why! :(
– Prashanth Benny
Mar 7 at 6:26
1
@PrashanthBenny: No I didn't downvote. Why would I answer a question if I downvoted. Also, getting downvotes on questions in regex tag is a normal trend. Anyway, take my upvote as your question is good and you showed what you tried with samples.
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:28
1
Thank you so very much for your support Pushpesh. This just saved my day from being screwed up with this issue... I had not actually looked at B as a possibility at all :D
– Prashanth Benny
Mar 7 at 9:58
add a comment |
1
Thanks @Pushpesh! working like a charm... :)
– Prashanth Benny
Mar 7 at 6:24
1
Pleased to help :)
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:25
1
did you downvote the question? Someone did! I dont undestand why! :(
– Prashanth Benny
Mar 7 at 6:26
1
@PrashanthBenny: No I didn't downvote. Why would I answer a question if I downvoted. Also, getting downvotes on questions in regex tag is a normal trend. Anyway, take my upvote as your question is good and you showed what you tried with samples.
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:28
1
Thank you so very much for your support Pushpesh. This just saved my day from being screwed up with this issue... I had not actually looked at B as a possibility at all :D
– Prashanth Benny
Mar 7 at 9:58
1
1
Thanks @Pushpesh! working like a charm... :)
– Prashanth Benny
Mar 7 at 6:24
Thanks @Pushpesh! working like a charm... :)
– Prashanth Benny
Mar 7 at 6:24
1
1
Pleased to help :)
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:25
Pleased to help :)
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:25
1
1
did you downvote the question? Someone did! I dont undestand why! :(
– Prashanth Benny
Mar 7 at 6:26
did you downvote the question? Someone did! I dont undestand why! :(
– Prashanth Benny
Mar 7 at 6:26
1
1
@PrashanthBenny: No I didn't downvote. Why would I answer a question if I downvoted. Also, getting downvotes on questions in regex tag is a normal trend. Anyway, take my upvote as your question is good and you showed what you tried with samples.
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:28
@PrashanthBenny: No I didn't downvote. Why would I answer a question if I downvoted. Also, getting downvotes on questions in regex tag is a normal trend. Anyway, take my upvote as your question is good and you showed what you tried with samples.
– Pushpesh Kumar Rajwanshi
Mar 7 at 6:28
1
1
Thank you so very much for your support Pushpesh. This just saved my day from being screwed up with this issue... I had not actually looked at B as a possibility at all :D
– Prashanth Benny
Mar 7 at 9:58
Thank you so very much for your support Pushpesh. This just saved my day from being screwed up with this issue... I had not actually looked at B as a possibility at all :D
– Prashanth Benny
Mar 7 at 9:58
add a comment |
For the string you have provided, you can use the regex:
B'K(?:(?!'B).)+
Click for Demo
Explanation:
B
- a non-word boundary'
- matches a'
K
- forget everything matched so far(?:(?!'B).)+
- matches 1+ occurrences of any character(except newline) which does not start with'
followed by a non-word boundary
1
Wow,K
. Didn't know about it for years... Thanks!
– 1234ru
Mar 7 at 12:23
add a comment |
For the string you have provided, you can use the regex:
B'K(?:(?!'B).)+
Click for Demo
Explanation:
B
- a non-word boundary'
- matches a'
K
- forget everything matched so far(?:(?!'B).)+
- matches 1+ occurrences of any character(except newline) which does not start with'
followed by a non-word boundary
1
Wow,K
. Didn't know about it for years... Thanks!
– 1234ru
Mar 7 at 12:23
add a comment |
For the string you have provided, you can use the regex:
B'K(?:(?!'B).)+
Click for Demo
Explanation:
B
- a non-word boundary'
- matches a'
K
- forget everything matched so far(?:(?!'B).)+
- matches 1+ occurrences of any character(except newline) which does not start with'
followed by a non-word boundary
For the string you have provided, you can use the regex:
B'K(?:(?!'B).)+
Click for Demo
Explanation:
B
- a non-word boundary'
- matches a'
K
- forget everything matched so far(?:(?!'B).)+
- matches 1+ occurrences of any character(except newline) which does not start with'
followed by a non-word boundary
answered Mar 7 at 5:24
PotatoPotato
7,76321133
7,76321133
1
Wow,K
. Didn't know about it for years... Thanks!
– 1234ru
Mar 7 at 12:23
add a comment |
1
Wow,K
. Didn't know about it for years... Thanks!
– 1234ru
Mar 7 at 12:23
1
1
Wow,
K
. Didn't know about it for years... Thanks!– 1234ru
Mar 7 at 12:23
Wow,
K
. Didn't know about it for years... Thanks!– 1234ru
Mar 7 at 12:23
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%2f55036563%2fignoring-apostrophe-while-capturing-contents-in-single-quotes-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