Regular expression with full match only












2















I know that this hackneyed theme and I read related answers...

However, I will ask...

I have input string in next format:



3#0.01, 2#0.5, 1#-10, -2#~ 


So this is a list of values splitted by comma.

I can catch all values with next expression:



(([-+]?d+)#([-+]?d*.?d+|~))+ 


It's working fine.



But I want to have no matching if any mistake is presenting in input string, for example:



MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO 


Unfortunately ^ and $ symbols are not helping here.

So my question: how can I stop matching if some part of input string is invalid.

Thanks.



Here is snippet: https://regex101.com/r/Xih0Qk/2










share|improve this question

























  • Try (([-+]?bd+)#([-+]?d*.?d+|~B))+

    – Wiktor Stribiżew
    Nov 21 '18 at 17:13











  • Wiktor, thanks for your attempt. But, unfortunately, no.

    – Sergey Alikin
    Nov 21 '18 at 17:49











  • So, you want to match them all only after validating the whole string? Something like regex101.com/r/Xih0Qk/3?

    – Wiktor Stribiżew
    Nov 21 '18 at 17:59











  • Wiktor, that's exactly what I want.

    – Sergey Alikin
    Nov 21 '18 at 18:13











  • Wiktor, wielkie dzięki, if I have guessed ;)

    – Sergey Alikin
    Nov 21 '18 at 18:14
















2















I know that this hackneyed theme and I read related answers...

However, I will ask...

I have input string in next format:



3#0.01, 2#0.5, 1#-10, -2#~ 


So this is a list of values splitted by comma.

I can catch all values with next expression:



(([-+]?d+)#([-+]?d*.?d+|~))+ 


It's working fine.



But I want to have no matching if any mistake is presenting in input string, for example:



MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO 


Unfortunately ^ and $ symbols are not helping here.

So my question: how can I stop matching if some part of input string is invalid.

Thanks.



Here is snippet: https://regex101.com/r/Xih0Qk/2










share|improve this question

























  • Try (([-+]?bd+)#([-+]?d*.?d+|~B))+

    – Wiktor Stribiżew
    Nov 21 '18 at 17:13











  • Wiktor, thanks for your attempt. But, unfortunately, no.

    – Sergey Alikin
    Nov 21 '18 at 17:49











  • So, you want to match them all only after validating the whole string? Something like regex101.com/r/Xih0Qk/3?

    – Wiktor Stribiżew
    Nov 21 '18 at 17:59











  • Wiktor, that's exactly what I want.

    – Sergey Alikin
    Nov 21 '18 at 18:13











  • Wiktor, wielkie dzięki, if I have guessed ;)

    – Sergey Alikin
    Nov 21 '18 at 18:14














2












2








2








I know that this hackneyed theme and I read related answers...

However, I will ask...

I have input string in next format:



3#0.01, 2#0.5, 1#-10, -2#~ 


So this is a list of values splitted by comma.

I can catch all values with next expression:



(([-+]?d+)#([-+]?d*.?d+|~))+ 


It's working fine.



But I want to have no matching if any mistake is presenting in input string, for example:



MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO 


Unfortunately ^ and $ symbols are not helping here.

So my question: how can I stop matching if some part of input string is invalid.

Thanks.



Here is snippet: https://regex101.com/r/Xih0Qk/2










share|improve this question
















I know that this hackneyed theme and I read related answers...

However, I will ask...

I have input string in next format:



3#0.01, 2#0.5, 1#-10, -2#~ 


So this is a list of values splitted by comma.

I can catch all values with next expression:



(([-+]?d+)#([-+]?d*.?d+|~))+ 


It's working fine.



But I want to have no matching if any mistake is presenting in input string, for example:



MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO 


Unfortunately ^ and $ symbols are not helping here.

So my question: how can I stop matching if some part of input string is invalid.

Thanks.



Here is snippet: https://regex101.com/r/Xih0Qk/2







.net regex lookahead lookbehind






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:52









Poul Bak

5,46831232




5,46831232










asked Nov 21 '18 at 17:12









Sergey AlikinSergey Alikin

305




305













  • Try (([-+]?bd+)#([-+]?d*.?d+|~B))+

    – Wiktor Stribiżew
    Nov 21 '18 at 17:13











  • Wiktor, thanks for your attempt. But, unfortunately, no.

    – Sergey Alikin
    Nov 21 '18 at 17:49











  • So, you want to match them all only after validating the whole string? Something like regex101.com/r/Xih0Qk/3?

    – Wiktor Stribiżew
    Nov 21 '18 at 17:59











  • Wiktor, that's exactly what I want.

    – Sergey Alikin
    Nov 21 '18 at 18:13











  • Wiktor, wielkie dzięki, if I have guessed ;)

    – Sergey Alikin
    Nov 21 '18 at 18:14



















  • Try (([-+]?bd+)#([-+]?d*.?d+|~B))+

    – Wiktor Stribiżew
    Nov 21 '18 at 17:13











  • Wiktor, thanks for your attempt. But, unfortunately, no.

    – Sergey Alikin
    Nov 21 '18 at 17:49











  • So, you want to match them all only after validating the whole string? Something like regex101.com/r/Xih0Qk/3?

    – Wiktor Stribiżew
    Nov 21 '18 at 17:59











  • Wiktor, that's exactly what I want.

    – Sergey Alikin
    Nov 21 '18 at 18:13











  • Wiktor, wielkie dzięki, if I have guessed ;)

    – Sergey Alikin
    Nov 21 '18 at 18:14

















Try (([-+]?bd+)#([-+]?d*.?d+|~B))+

– Wiktor Stribiżew
Nov 21 '18 at 17:13





Try (([-+]?bd+)#([-+]?d*.?d+|~B))+

– Wiktor Stribiżew
Nov 21 '18 at 17:13













Wiktor, thanks for your attempt. But, unfortunately, no.

– Sergey Alikin
Nov 21 '18 at 17:49





Wiktor, thanks for your attempt. But, unfortunately, no.

– Sergey Alikin
Nov 21 '18 at 17:49













So, you want to match them all only after validating the whole string? Something like regex101.com/r/Xih0Qk/3?

– Wiktor Stribiżew
Nov 21 '18 at 17:59





So, you want to match them all only after validating the whole string? Something like regex101.com/r/Xih0Qk/3?

– Wiktor Stribiżew
Nov 21 '18 at 17:59













Wiktor, that's exactly what I want.

– Sergey Alikin
Nov 21 '18 at 18:13





Wiktor, that's exactly what I want.

– Sergey Alikin
Nov 21 '18 at 18:13













Wiktor, wielkie dzięki, if I have guessed ;)

– Sergey Alikin
Nov 21 '18 at 18:14





Wiktor, wielkie dzięki, if I have guessed ;)

– Sergey Alikin
Nov 21 '18 at 18:14












1 Answer
1






active

oldest

votes


















2














In .NET, you may use a regex with inifinite width lookbehinds (it is also supported in the latest ECMAScript 2018 powered JavaScript environments, if you need to port the same solution there). The regex will look like



(?<=^(?:[-+]?d+#(?:[-+]?d*.?d+|~),s)*)[-+]?d+#(?:[-+]?d*.?d+|~)(?=(?:,s[-+]?d+#(?:[-+]?d*.?d+|~))*$)


See the online regex demo



In code, it is easier to build the pattern from a variable:



var block = @"[-+]?d+#(?:[-+]?d*.?d+|~)";   // Block/unit pattern
var pattern = $@"(?<=^(?:{block},s)*){block}(?=(?:,s{block})*$)";
var results1 = Regex.Matches("3#0.01, 2#0.5, 1#-10, -2#~", pattern)
.Cast<Match>().Select(x => x.Value);
if (results1.Count() > 0)
Console.WriteLine(string.Join(", ", results1));
var results2 = Regex.Matches("MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO", pattern)
.Cast<Match>().Select(x => x.Value);
if (results2.Count() > 0)
Console.WriteLine(string.Join(", ", results2));


See the C# demo online. Output (only the right string matched):



3#0.01, 2#0.5, 1#-10, -2#~


Pattern explanation





  • (?<=^(?:{block},s)*) - a positive lookbehind that only matches a location immediately preceded with 0+ occurrences of a {block} pattern at the start of the string followed with a , and 1 whitespace


  • {block} - your block/unit pattern to match


  • (?=(?:,s{block})*$) - a positive lookahead that matches a location immediately followed with 0+ occurrences of a ,, a whitespace and a {block} pattern up to the end of the string.






share|improve this answer
























  • Wiktor, thanks! You are the best. Can I ask about small detail: how can I match only WRONG patterns. MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO. First and last in this line, for example.

    – Sergey Alikin
    Nov 21 '18 at 19:00











  • @SergeyAlikin Probably, if you remove all the valid ones, Regex.Replace(s, @"(?:^|,s)[-+]?d+#(?:[-+]?d*.?d+|~)(?=,s|$)", "").Split(new {", "}, StringSplitOptions.None)

    – Wiktor Stribiżew
    Nov 21 '18 at 19:05











  • Wiktor, thank you very very much. I will not disturb you more.

    – Sergey Alikin
    Nov 21 '18 at 19:17











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417330%2fregular-expression-with-full-match-only%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









2














In .NET, you may use a regex with inifinite width lookbehinds (it is also supported in the latest ECMAScript 2018 powered JavaScript environments, if you need to port the same solution there). The regex will look like



(?<=^(?:[-+]?d+#(?:[-+]?d*.?d+|~),s)*)[-+]?d+#(?:[-+]?d*.?d+|~)(?=(?:,s[-+]?d+#(?:[-+]?d*.?d+|~))*$)


See the online regex demo



In code, it is easier to build the pattern from a variable:



var block = @"[-+]?d+#(?:[-+]?d*.?d+|~)";   // Block/unit pattern
var pattern = $@"(?<=^(?:{block},s)*){block}(?=(?:,s{block})*$)";
var results1 = Regex.Matches("3#0.01, 2#0.5, 1#-10, -2#~", pattern)
.Cast<Match>().Select(x => x.Value);
if (results1.Count() > 0)
Console.WriteLine(string.Join(", ", results1));
var results2 = Regex.Matches("MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO", pattern)
.Cast<Match>().Select(x => x.Value);
if (results2.Count() > 0)
Console.WriteLine(string.Join(", ", results2));


See the C# demo online. Output (only the right string matched):



3#0.01, 2#0.5, 1#-10, -2#~


Pattern explanation





  • (?<=^(?:{block},s)*) - a positive lookbehind that only matches a location immediately preceded with 0+ occurrences of a {block} pattern at the start of the string followed with a , and 1 whitespace


  • {block} - your block/unit pattern to match


  • (?=(?:,s{block})*$) - a positive lookahead that matches a location immediately followed with 0+ occurrences of a ,, a whitespace and a {block} pattern up to the end of the string.






share|improve this answer
























  • Wiktor, thanks! You are the best. Can I ask about small detail: how can I match only WRONG patterns. MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO. First and last in this line, for example.

    – Sergey Alikin
    Nov 21 '18 at 19:00











  • @SergeyAlikin Probably, if you remove all the valid ones, Regex.Replace(s, @"(?:^|,s)[-+]?d+#(?:[-+]?d*.?d+|~)(?=,s|$)", "").Split(new {", "}, StringSplitOptions.None)

    – Wiktor Stribiżew
    Nov 21 '18 at 19:05











  • Wiktor, thank you very very much. I will not disturb you more.

    – Sergey Alikin
    Nov 21 '18 at 19:17
















2














In .NET, you may use a regex with inifinite width lookbehinds (it is also supported in the latest ECMAScript 2018 powered JavaScript environments, if you need to port the same solution there). The regex will look like



(?<=^(?:[-+]?d+#(?:[-+]?d*.?d+|~),s)*)[-+]?d+#(?:[-+]?d*.?d+|~)(?=(?:,s[-+]?d+#(?:[-+]?d*.?d+|~))*$)


See the online regex demo



In code, it is easier to build the pattern from a variable:



var block = @"[-+]?d+#(?:[-+]?d*.?d+|~)";   // Block/unit pattern
var pattern = $@"(?<=^(?:{block},s)*){block}(?=(?:,s{block})*$)";
var results1 = Regex.Matches("3#0.01, 2#0.5, 1#-10, -2#~", pattern)
.Cast<Match>().Select(x => x.Value);
if (results1.Count() > 0)
Console.WriteLine(string.Join(", ", results1));
var results2 = Regex.Matches("MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO", pattern)
.Cast<Match>().Select(x => x.Value);
if (results2.Count() > 0)
Console.WriteLine(string.Join(", ", results2));


See the C# demo online. Output (only the right string matched):



3#0.01, 2#0.5, 1#-10, -2#~


Pattern explanation





  • (?<=^(?:{block},s)*) - a positive lookbehind that only matches a location immediately preceded with 0+ occurrences of a {block} pattern at the start of the string followed with a , and 1 whitespace


  • {block} - your block/unit pattern to match


  • (?=(?:,s{block})*$) - a positive lookahead that matches a location immediately followed with 0+ occurrences of a ,, a whitespace and a {block} pattern up to the end of the string.






share|improve this answer
























  • Wiktor, thanks! You are the best. Can I ask about small detail: how can I match only WRONG patterns. MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO. First and last in this line, for example.

    – Sergey Alikin
    Nov 21 '18 at 19:00











  • @SergeyAlikin Probably, if you remove all the valid ones, Regex.Replace(s, @"(?:^|,s)[-+]?d+#(?:[-+]?d*.?d+|~)(?=,s|$)", "").Split(new {", "}, StringSplitOptions.None)

    – Wiktor Stribiżew
    Nov 21 '18 at 19:05











  • Wiktor, thank you very very much. I will not disturb you more.

    – Sergey Alikin
    Nov 21 '18 at 19:17














2












2








2







In .NET, you may use a regex with inifinite width lookbehinds (it is also supported in the latest ECMAScript 2018 powered JavaScript environments, if you need to port the same solution there). The regex will look like



(?<=^(?:[-+]?d+#(?:[-+]?d*.?d+|~),s)*)[-+]?d+#(?:[-+]?d*.?d+|~)(?=(?:,s[-+]?d+#(?:[-+]?d*.?d+|~))*$)


See the online regex demo



In code, it is easier to build the pattern from a variable:



var block = @"[-+]?d+#(?:[-+]?d*.?d+|~)";   // Block/unit pattern
var pattern = $@"(?<=^(?:{block},s)*){block}(?=(?:,s{block})*$)";
var results1 = Regex.Matches("3#0.01, 2#0.5, 1#-10, -2#~", pattern)
.Cast<Match>().Select(x => x.Value);
if (results1.Count() > 0)
Console.WriteLine(string.Join(", ", results1));
var results2 = Regex.Matches("MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO", pattern)
.Cast<Match>().Select(x => x.Value);
if (results2.Count() > 0)
Console.WriteLine(string.Join(", ", results2));


See the C# demo online. Output (only the right string matched):



3#0.01, 2#0.5, 1#-10, -2#~


Pattern explanation





  • (?<=^(?:{block},s)*) - a positive lookbehind that only matches a location immediately preceded with 0+ occurrences of a {block} pattern at the start of the string followed with a , and 1 whitespace


  • {block} - your block/unit pattern to match


  • (?=(?:,s{block})*$) - a positive lookahead that matches a location immediately followed with 0+ occurrences of a ,, a whitespace and a {block} pattern up to the end of the string.






share|improve this answer













In .NET, you may use a regex with inifinite width lookbehinds (it is also supported in the latest ECMAScript 2018 powered JavaScript environments, if you need to port the same solution there). The regex will look like



(?<=^(?:[-+]?d+#(?:[-+]?d*.?d+|~),s)*)[-+]?d+#(?:[-+]?d*.?d+|~)(?=(?:,s[-+]?d+#(?:[-+]?d*.?d+|~))*$)


See the online regex demo



In code, it is easier to build the pattern from a variable:



var block = @"[-+]?d+#(?:[-+]?d*.?d+|~)";   // Block/unit pattern
var pattern = $@"(?<=^(?:{block},s)*){block}(?=(?:,s{block})*$)";
var results1 = Regex.Matches("3#0.01, 2#0.5, 1#-10, -2#~", pattern)
.Cast<Match>().Select(x => x.Value);
if (results1.Count() > 0)
Console.WriteLine(string.Join(", ", results1));
var results2 = Regex.Matches("MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO", pattern)
.Cast<Match>().Select(x => x.Value);
if (results2.Count() > 0)
Console.WriteLine(string.Join(", ", results2));


See the C# demo online. Output (only the right string matched):



3#0.01, 2#0.5, 1#-10, -2#~


Pattern explanation





  • (?<=^(?:{block},s)*) - a positive lookbehind that only matches a location immediately preceded with 0+ occurrences of a {block} pattern at the start of the string followed with a , and 1 whitespace


  • {block} - your block/unit pattern to match


  • (?=(?:,s{block})*$) - a positive lookahead that matches a location immediately followed with 0+ occurrences of a ,, a whitespace and a {block} pattern up to the end of the string.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 18:48









Wiktor StribiżewWiktor Stribiżew

315k16133213




315k16133213













  • Wiktor, thanks! You are the best. Can I ask about small detail: how can I match only WRONG patterns. MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO. First and last in this line, for example.

    – Sergey Alikin
    Nov 21 '18 at 19:00











  • @SergeyAlikin Probably, if you remove all the valid ones, Regex.Replace(s, @"(?:^|,s)[-+]?d+#(?:[-+]?d*.?d+|~)(?=,s|$)", "").Split(new {", "}, StringSplitOptions.None)

    – Wiktor Stribiżew
    Nov 21 '18 at 19:05











  • Wiktor, thank you very very much. I will not disturb you more.

    – Sergey Alikin
    Nov 21 '18 at 19:17



















  • Wiktor, thanks! You are the best. Can I ask about small detail: how can I match only WRONG patterns. MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO. First and last in this line, for example.

    – Sergey Alikin
    Nov 21 '18 at 19:00











  • @SergeyAlikin Probably, if you remove all the valid ones, Regex.Replace(s, @"(?:^|,s)[-+]?d+#(?:[-+]?d*.?d+|~)(?=,s|$)", "").Split(new {", "}, StringSplitOptions.None)

    – Wiktor Stribiżew
    Nov 21 '18 at 19:05











  • Wiktor, thank you very very much. I will not disturb you more.

    – Sergey Alikin
    Nov 21 '18 at 19:17

















Wiktor, thanks! You are the best. Can I ask about small detail: how can I match only WRONG patterns. MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO. First and last in this line, for example.

– Sergey Alikin
Nov 21 '18 at 19:00





Wiktor, thanks! You are the best. Can I ask about small detail: how can I match only WRONG patterns. MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO. First and last in this line, for example.

– Sergey Alikin
Nov 21 '18 at 19:00













@SergeyAlikin Probably, if you remove all the valid ones, Regex.Replace(s, @"(?:^|,s)[-+]?d+#(?:[-+]?d*.?d+|~)(?=,s|$)", "").Split(new {", "}, StringSplitOptions.None)

– Wiktor Stribiżew
Nov 21 '18 at 19:05





@SergeyAlikin Probably, if you remove all the valid ones, Regex.Replace(s, @"(?:^|,s)[-+]?d+#(?:[-+]?d*.?d+|~)(?=,s|$)", "").Split(new {", "}, StringSplitOptions.None)

– Wiktor Stribiżew
Nov 21 '18 at 19:05













Wiktor, thank you very very much. I will not disturb you more.

– Sergey Alikin
Nov 21 '18 at 19:17





Wiktor, thank you very very much. I will not disturb you more.

– Sergey Alikin
Nov 21 '18 at 19:17


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417330%2fregular-expression-with-full-match-only%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy