Regex (.NET) Capture Everything But Spaces
I am attempting to use a regular expression with a single capture group to turn the following:
[RuleID("MC304")]
[RuleID("MC 304")]
[RuleID("MC 30 4")]
into this:
[RuleID("MC304")]
[RuleID("MC304")]
[RuleID("MC304")]
I have accomplished this result using this regex101 example but it is pretty rudimentary:
I matched
[RuleID("(w*)s*(w*)s*(w*)s*")]
and replaced with
[RuleID("$1$2$3")]
I would like to be able to use non-capturing groups nested inside of a capture group so I can just replace the content inside of the double quotes with $1
instead of having to concatenate $1$2$3
but I don't believe this is possible.
If anyone has any guidance on how to write a regular expression that is more reusable than having to duplicate (w*)s*
for every space I expect I'd greatly appreciate the help!
Edit
I am wanting this solution to be generic for any alphanumeric string with spaces in between. The example above is for "MC304" but it could just as well be any number letter combination with spaces.
Note: I am using Visual Studio's built in find and replace feature to remove spaces from all RuleID
attributes across the entire solution.
.net regex
add a comment |
I am attempting to use a regular expression with a single capture group to turn the following:
[RuleID("MC304")]
[RuleID("MC 304")]
[RuleID("MC 30 4")]
into this:
[RuleID("MC304")]
[RuleID("MC304")]
[RuleID("MC304")]
I have accomplished this result using this regex101 example but it is pretty rudimentary:
I matched
[RuleID("(w*)s*(w*)s*(w*)s*")]
and replaced with
[RuleID("$1$2$3")]
I would like to be able to use non-capturing groups nested inside of a capture group so I can just replace the content inside of the double quotes with $1
instead of having to concatenate $1$2$3
but I don't believe this is possible.
If anyone has any guidance on how to write a regular expression that is more reusable than having to duplicate (w*)s*
for every space I expect I'd greatly appreciate the help!
Edit
I am wanting this solution to be generic for any alphanumeric string with spaces in between. The example above is for "MC304" but it could just as well be any number letter combination with spaces.
Note: I am using Visual Studio's built in find and replace feature to remove spaces from all RuleID
attributes across the entire solution.
.net regex
That website is really, really slow to load for me (waited several minutes and it still hasn't finished). Even if that weren't the case, could you edit your question to include the pattern you used so all the pertinent information is present in the question itself and not dependent upon external resources?
– BACON
Nov 14 '18 at 2:30
@BACON Might be a problem closer to your end, loaded fine for me, and regex101 is generally pretty reliable
– CertainPerformance
Nov 14 '18 at 2:33
Depends on your environment, but might you match the whole "MC 30 4" part, use another regex to remove all spaces from it, and replace the original string with that result?
– CertainPerformance
Nov 14 '18 at 2:37
1
Not to be a downer, but why would you use RegEx for this task in the first place? Seems like a lot of overhead... Why not use string.Replace(' ', "")?
– Drunken Code Monkey
Nov 14 '18 at 2:39
@DrunkenCodeMonkey Good question. I'm attempting to use Visual Studio's find and replace function to normalize an attribute across an existing solution.
– SupposedlySam
Nov 14 '18 at 3:19
add a comment |
I am attempting to use a regular expression with a single capture group to turn the following:
[RuleID("MC304")]
[RuleID("MC 304")]
[RuleID("MC 30 4")]
into this:
[RuleID("MC304")]
[RuleID("MC304")]
[RuleID("MC304")]
I have accomplished this result using this regex101 example but it is pretty rudimentary:
I matched
[RuleID("(w*)s*(w*)s*(w*)s*")]
and replaced with
[RuleID("$1$2$3")]
I would like to be able to use non-capturing groups nested inside of a capture group so I can just replace the content inside of the double quotes with $1
instead of having to concatenate $1$2$3
but I don't believe this is possible.
If anyone has any guidance on how to write a regular expression that is more reusable than having to duplicate (w*)s*
for every space I expect I'd greatly appreciate the help!
Edit
I am wanting this solution to be generic for any alphanumeric string with spaces in between. The example above is for "MC304" but it could just as well be any number letter combination with spaces.
Note: I am using Visual Studio's built in find and replace feature to remove spaces from all RuleID
attributes across the entire solution.
.net regex
I am attempting to use a regular expression with a single capture group to turn the following:
[RuleID("MC304")]
[RuleID("MC 304")]
[RuleID("MC 30 4")]
into this:
[RuleID("MC304")]
[RuleID("MC304")]
[RuleID("MC304")]
I have accomplished this result using this regex101 example but it is pretty rudimentary:
I matched
[RuleID("(w*)s*(w*)s*(w*)s*")]
and replaced with
[RuleID("$1$2$3")]
I would like to be able to use non-capturing groups nested inside of a capture group so I can just replace the content inside of the double quotes with $1
instead of having to concatenate $1$2$3
but I don't believe this is possible.
If anyone has any guidance on how to write a regular expression that is more reusable than having to duplicate (w*)s*
for every space I expect I'd greatly appreciate the help!
Edit
I am wanting this solution to be generic for any alphanumeric string with spaces in between. The example above is for "MC304" but it could just as well be any number letter combination with spaces.
Note: I am using Visual Studio's built in find and replace feature to remove spaces from all RuleID
attributes across the entire solution.
.net regex
.net regex
edited Nov 14 '18 at 3:56
SupposedlySam
asked Nov 14 '18 at 2:17
SupposedlySamSupposedlySam
18119
18119
That website is really, really slow to load for me (waited several minutes and it still hasn't finished). Even if that weren't the case, could you edit your question to include the pattern you used so all the pertinent information is present in the question itself and not dependent upon external resources?
– BACON
Nov 14 '18 at 2:30
@BACON Might be a problem closer to your end, loaded fine for me, and regex101 is generally pretty reliable
– CertainPerformance
Nov 14 '18 at 2:33
Depends on your environment, but might you match the whole "MC 30 4" part, use another regex to remove all spaces from it, and replace the original string with that result?
– CertainPerformance
Nov 14 '18 at 2:37
1
Not to be a downer, but why would you use RegEx for this task in the first place? Seems like a lot of overhead... Why not use string.Replace(' ', "")?
– Drunken Code Monkey
Nov 14 '18 at 2:39
@DrunkenCodeMonkey Good question. I'm attempting to use Visual Studio's find and replace function to normalize an attribute across an existing solution.
– SupposedlySam
Nov 14 '18 at 3:19
add a comment |
That website is really, really slow to load for me (waited several minutes and it still hasn't finished). Even if that weren't the case, could you edit your question to include the pattern you used so all the pertinent information is present in the question itself and not dependent upon external resources?
– BACON
Nov 14 '18 at 2:30
@BACON Might be a problem closer to your end, loaded fine for me, and regex101 is generally pretty reliable
– CertainPerformance
Nov 14 '18 at 2:33
Depends on your environment, but might you match the whole "MC 30 4" part, use another regex to remove all spaces from it, and replace the original string with that result?
– CertainPerformance
Nov 14 '18 at 2:37
1
Not to be a downer, but why would you use RegEx for this task in the first place? Seems like a lot of overhead... Why not use string.Replace(' ', "")?
– Drunken Code Monkey
Nov 14 '18 at 2:39
@DrunkenCodeMonkey Good question. I'm attempting to use Visual Studio's find and replace function to normalize an attribute across an existing solution.
– SupposedlySam
Nov 14 '18 at 3:19
That website is really, really slow to load for me (waited several minutes and it still hasn't finished). Even if that weren't the case, could you edit your question to include the pattern you used so all the pertinent information is present in the question itself and not dependent upon external resources?
– BACON
Nov 14 '18 at 2:30
That website is really, really slow to load for me (waited several minutes and it still hasn't finished). Even if that weren't the case, could you edit your question to include the pattern you used so all the pertinent information is present in the question itself and not dependent upon external resources?
– BACON
Nov 14 '18 at 2:30
@BACON Might be a problem closer to your end, loaded fine for me, and regex101 is generally pretty reliable
– CertainPerformance
Nov 14 '18 at 2:33
@BACON Might be a problem closer to your end, loaded fine for me, and regex101 is generally pretty reliable
– CertainPerformance
Nov 14 '18 at 2:33
Depends on your environment, but might you match the whole "MC 30 4" part, use another regex to remove all spaces from it, and replace the original string with that result?
– CertainPerformance
Nov 14 '18 at 2:37
Depends on your environment, but might you match the whole "MC 30 4" part, use another regex to remove all spaces from it, and replace the original string with that result?
– CertainPerformance
Nov 14 '18 at 2:37
1
1
Not to be a downer, but why would you use RegEx for this task in the first place? Seems like a lot of overhead... Why not use string.Replace(' ', "")?
– Drunken Code Monkey
Nov 14 '18 at 2:39
Not to be a downer, but why would you use RegEx for this task in the first place? Seems like a lot of overhead... Why not use string.Replace(' ', "")?
– Drunken Code Monkey
Nov 14 '18 at 2:39
@DrunkenCodeMonkey Good question. I'm attempting to use Visual Studio's find and replace function to normalize an attribute across an existing solution.
– SupposedlySam
Nov 14 '18 at 3:19
@DrunkenCodeMonkey Good question. I'm attempting to use Visual Studio's find and replace function to normalize an attribute across an existing solution.
– SupposedlySam
Nov 14 '18 at 3:19
add a comment |
1 Answer
1
active
oldest
votes
After clarifications in the comments on the usage, you can use this pattern to match your strings. Note that you need to provide the length (without spaces) of your search string, and the search string itself (again without the spaces).
([mc304](?:s+?)?)5
This works by using a non capturing group to isolate spaces, and by using a character class to specify which characters are legal. Note that you should not be repeating letters or numbers in there, so if you are looking for "mc300", you would use [mc30] and a length of 5. Unfortunately this will also match mc030 and mc003 etc... But maybe you can work with that by making sure manually before pressing replace...
I edited the question above. Based on what was initially given and my clarifications this is a good answer but doesn't quite do what I'm looking for. I need a solution that can be agnostic to the string that is being replaced. Any ideas?
– SupposedlySam
Nov 14 '18 at 3:59
This is pretty much the closest I was able to get, but it seems good enough to do a manual search and replace to me... Of course please don't do a replace all or you will be sorry. I don't think there is any way to do exactly what you want, but this method will get you AT LEAST the results you want (plus many you don't want).
– Drunken Code Monkey
Nov 14 '18 at 4:02
Would you be able to send me a link of it actually working? It doesn't seem to be matching the way I expected it to when I used my regex101 example with this.
– SupposedlySam
Nov 14 '18 at 4:06
I just tested it in visual studio. But as I said it's not really robust as it is...
– Drunken Code Monkey
Nov 14 '18 at 4:45
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%2f53292257%2fregex-net-capture-everything-but-spaces%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
After clarifications in the comments on the usage, you can use this pattern to match your strings. Note that you need to provide the length (without spaces) of your search string, and the search string itself (again without the spaces).
([mc304](?:s+?)?)5
This works by using a non capturing group to isolate spaces, and by using a character class to specify which characters are legal. Note that you should not be repeating letters or numbers in there, so if you are looking for "mc300", you would use [mc30] and a length of 5. Unfortunately this will also match mc030 and mc003 etc... But maybe you can work with that by making sure manually before pressing replace...
I edited the question above. Based on what was initially given and my clarifications this is a good answer but doesn't quite do what I'm looking for. I need a solution that can be agnostic to the string that is being replaced. Any ideas?
– SupposedlySam
Nov 14 '18 at 3:59
This is pretty much the closest I was able to get, but it seems good enough to do a manual search and replace to me... Of course please don't do a replace all or you will be sorry. I don't think there is any way to do exactly what you want, but this method will get you AT LEAST the results you want (plus many you don't want).
– Drunken Code Monkey
Nov 14 '18 at 4:02
Would you be able to send me a link of it actually working? It doesn't seem to be matching the way I expected it to when I used my regex101 example with this.
– SupposedlySam
Nov 14 '18 at 4:06
I just tested it in visual studio. But as I said it's not really robust as it is...
– Drunken Code Monkey
Nov 14 '18 at 4:45
add a comment |
After clarifications in the comments on the usage, you can use this pattern to match your strings. Note that you need to provide the length (without spaces) of your search string, and the search string itself (again without the spaces).
([mc304](?:s+?)?)5
This works by using a non capturing group to isolate spaces, and by using a character class to specify which characters are legal. Note that you should not be repeating letters or numbers in there, so if you are looking for "mc300", you would use [mc30] and a length of 5. Unfortunately this will also match mc030 and mc003 etc... But maybe you can work with that by making sure manually before pressing replace...
I edited the question above. Based on what was initially given and my clarifications this is a good answer but doesn't quite do what I'm looking for. I need a solution that can be agnostic to the string that is being replaced. Any ideas?
– SupposedlySam
Nov 14 '18 at 3:59
This is pretty much the closest I was able to get, but it seems good enough to do a manual search and replace to me... Of course please don't do a replace all or you will be sorry. I don't think there is any way to do exactly what you want, but this method will get you AT LEAST the results you want (plus many you don't want).
– Drunken Code Monkey
Nov 14 '18 at 4:02
Would you be able to send me a link of it actually working? It doesn't seem to be matching the way I expected it to when I used my regex101 example with this.
– SupposedlySam
Nov 14 '18 at 4:06
I just tested it in visual studio. But as I said it's not really robust as it is...
– Drunken Code Monkey
Nov 14 '18 at 4:45
add a comment |
After clarifications in the comments on the usage, you can use this pattern to match your strings. Note that you need to provide the length (without spaces) of your search string, and the search string itself (again without the spaces).
([mc304](?:s+?)?)5
This works by using a non capturing group to isolate spaces, and by using a character class to specify which characters are legal. Note that you should not be repeating letters or numbers in there, so if you are looking for "mc300", you would use [mc30] and a length of 5. Unfortunately this will also match mc030 and mc003 etc... But maybe you can work with that by making sure manually before pressing replace...
After clarifications in the comments on the usage, you can use this pattern to match your strings. Note that you need to provide the length (without spaces) of your search string, and the search string itself (again without the spaces).
([mc304](?:s+?)?)5
This works by using a non capturing group to isolate spaces, and by using a character class to specify which characters are legal. Note that you should not be repeating letters or numbers in there, so if you are looking for "mc300", you would use [mc30] and a length of 5. Unfortunately this will also match mc030 and mc003 etc... But maybe you can work with that by making sure manually before pressing replace...
edited Nov 14 '18 at 3:43
answered Nov 14 '18 at 3:36
Drunken Code MonkeyDrunken Code Monkey
1,3681615
1,3681615
I edited the question above. Based on what was initially given and my clarifications this is a good answer but doesn't quite do what I'm looking for. I need a solution that can be agnostic to the string that is being replaced. Any ideas?
– SupposedlySam
Nov 14 '18 at 3:59
This is pretty much the closest I was able to get, but it seems good enough to do a manual search and replace to me... Of course please don't do a replace all or you will be sorry. I don't think there is any way to do exactly what you want, but this method will get you AT LEAST the results you want (plus many you don't want).
– Drunken Code Monkey
Nov 14 '18 at 4:02
Would you be able to send me a link of it actually working? It doesn't seem to be matching the way I expected it to when I used my regex101 example with this.
– SupposedlySam
Nov 14 '18 at 4:06
I just tested it in visual studio. But as I said it's not really robust as it is...
– Drunken Code Monkey
Nov 14 '18 at 4:45
add a comment |
I edited the question above. Based on what was initially given and my clarifications this is a good answer but doesn't quite do what I'm looking for. I need a solution that can be agnostic to the string that is being replaced. Any ideas?
– SupposedlySam
Nov 14 '18 at 3:59
This is pretty much the closest I was able to get, but it seems good enough to do a manual search and replace to me... Of course please don't do a replace all or you will be sorry. I don't think there is any way to do exactly what you want, but this method will get you AT LEAST the results you want (plus many you don't want).
– Drunken Code Monkey
Nov 14 '18 at 4:02
Would you be able to send me a link of it actually working? It doesn't seem to be matching the way I expected it to when I used my regex101 example with this.
– SupposedlySam
Nov 14 '18 at 4:06
I just tested it in visual studio. But as I said it's not really robust as it is...
– Drunken Code Monkey
Nov 14 '18 at 4:45
I edited the question above. Based on what was initially given and my clarifications this is a good answer but doesn't quite do what I'm looking for. I need a solution that can be agnostic to the string that is being replaced. Any ideas?
– SupposedlySam
Nov 14 '18 at 3:59
I edited the question above. Based on what was initially given and my clarifications this is a good answer but doesn't quite do what I'm looking for. I need a solution that can be agnostic to the string that is being replaced. Any ideas?
– SupposedlySam
Nov 14 '18 at 3:59
This is pretty much the closest I was able to get, but it seems good enough to do a manual search and replace to me... Of course please don't do a replace all or you will be sorry. I don't think there is any way to do exactly what you want, but this method will get you AT LEAST the results you want (plus many you don't want).
– Drunken Code Monkey
Nov 14 '18 at 4:02
This is pretty much the closest I was able to get, but it seems good enough to do a manual search and replace to me... Of course please don't do a replace all or you will be sorry. I don't think there is any way to do exactly what you want, but this method will get you AT LEAST the results you want (plus many you don't want).
– Drunken Code Monkey
Nov 14 '18 at 4:02
Would you be able to send me a link of it actually working? It doesn't seem to be matching the way I expected it to when I used my regex101 example with this.
– SupposedlySam
Nov 14 '18 at 4:06
Would you be able to send me a link of it actually working? It doesn't seem to be matching the way I expected it to when I used my regex101 example with this.
– SupposedlySam
Nov 14 '18 at 4:06
I just tested it in visual studio. But as I said it's not really robust as it is...
– Drunken Code Monkey
Nov 14 '18 at 4:45
I just tested it in visual studio. But as I said it's not really robust as it is...
– Drunken Code Monkey
Nov 14 '18 at 4:45
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%2f53292257%2fregex-net-capture-everything-but-spaces%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
That website is really, really slow to load for me (waited several minutes and it still hasn't finished). Even if that weren't the case, could you edit your question to include the pattern you used so all the pertinent information is present in the question itself and not dependent upon external resources?
– BACON
Nov 14 '18 at 2:30
@BACON Might be a problem closer to your end, loaded fine for me, and regex101 is generally pretty reliable
– CertainPerformance
Nov 14 '18 at 2:33
Depends on your environment, but might you match the whole "MC 30 4" part, use another regex to remove all spaces from it, and replace the original string with that result?
– CertainPerformance
Nov 14 '18 at 2:37
1
Not to be a downer, but why would you use RegEx for this task in the first place? Seems like a lot of overhead... Why not use string.Replace(' ', "")?
– Drunken Code Monkey
Nov 14 '18 at 2:39
@DrunkenCodeMonkey Good question. I'm attempting to use Visual Studio's find and replace function to normalize an attribute across an existing solution.
– SupposedlySam
Nov 14 '18 at 3:19