Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, June 12, 2020 1:43 PM
I need to find/replace "," (comma) with ", " (comma and space) a large number of times but the replacement should only be made when the comma is not either followed by white space (for example a space or tab) or is at the end of a line. Working in C++ in Visual Studio either 2010 or 2013. It seems like a regular expression could be formed to do this but I don't know enough of the advanced options. Thanks for any help.
All replies (10)
Friday, June 12, 2020 8:34 PM
Hi John,
Wondering about a couple of background considerations here. Are you treating this text as a filestream or character array? There are two methods that pop in to mind to accomplish this. If you are using an array you can use a for loop to check the character in a current referenced position and you can perform an and statement to see if the character in the subsequent position is a space. You would have to perform some array shifting to accommodate for the new spaces you intent to insert. If you are using a filestream operation then you can use a combination of .get() and .peek() functions to check current character and next, then copy to a cached file and write back to the original.
Do you have a code snapshot of what you are trying to make work or are you more looking for an example of how to do one of these two operations?
Graphics post update.
Friday, June 12, 2020 8:41 PM
Neither, actually. By "Find/Replace", I meant the Visual Studio user interface find/replace operation for editing program text, which can accept a regular expression for what to find. I'm not looking for programmatic operation. I was guessing that the regular expression could affect what to find and replace in this way but I'm not sure if that's possible. Sorry if any of that was unclear. Thank you.
Monday, June 15, 2020 9:09 AM | 1 vote
Hi John,
Welcome to MSDN forum.
I test on my side by using Visual Studio 2019 Community v16.6.2, and try to find “,”(comma) replace with “, ”(comma and space) but it works well. Please kindly check the gif below. Feel free to let me know if some steps or conditions are missed.
Could you share me with some details like which version of VS do you use? I suggest you update VS to the latest version if you don’t use the latest version of VS currently.
>> It seems like a regular expression could be formed to do this but I don’t know enough of the advanced options.
## Yes, regular expressions could be formed to do this, but I think there is no need to use regular expressions.
## About regular expressions you can refer more from Use regular expressions in Visual Studio.
Feel free to contact me.
Best Regards,
Tianyu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Wednesday, June 17, 2020 11:56 AM
The replacement must not occur if the character following the comma is white space or end of line. I use either VS 2010 or 2013. All of this was stated in my original question.
Thursday, June 18, 2020 9:51 AM
Hi John Boncek,
I tried it on visual studio 2013, and could replace ',' to ', '.
Note: Go to Edit-> Advanced -> View white space
Could you provide some details about this issue on your side, for example related screenshots?
If there is any update, please feel free to contact us.
Best Regards,
Dylan
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]
Friday, June 19, 2020 9:59 AM
Hi John,
Thanks for your feedback.
I test again with VS 2010 and VS 2013, I notice that, on my side, if the character following the comma is tab, then replacement isn’t made, but if the character following the comma is white space or end of line, it works well.
I suggest you update VS 2010 and VS 2013 to the latest version(if you don’t use the latest version), and try to repair VS 2010 and VS 2013 from control panel, to see if the second condition could work.
And for the first condition(the character following the comma is tab), I think it’s a potential issue and I have reported it to VS Product Team, this is the link, you can go to vote, add comments and follow this thread.
Hope this helps.
Best Regards,
Tianyu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Friday, June 19, 2020 11:04 AM | 1 vote
Try using the following regular expression for the find -
,(?!\s)\b
Following is what the VS2013 editor displayed -
Friday, June 19, 2020 1:31 PM
Thank you. That works on many cases in a code file I tried, but in this line
HEC_ERROR_TEST_FMT(!SUCCEEDED(hr),(_T("Failed to Enter. hr = %08lx %s"),hr,(LPCTSTR)GetOLEErrorString(hr)),;)
it finds only the second occurrence. The others are not highlighted and are skipped over.
Friday, June 19, 2020 1:43 PM | 1 vote
Thank you. That works on many cases in a code file I tried, but in this line
HEC_ERROR_TEST_FMT(!SUCCEEDED(hr),(_T("Failed to Enter. hr = %08lx %s"),hr,(LPCTSTR)GetOLEErrorString(hr)),;)
it finds only the second occurrence. The others are not highlighted and are skipped over.
The \b at the end of the regular expression was used for the edge case where there is a comma at the end of a line that is not followed by a newline (i.e., carriage return/line feed).
If that is not an issue for you, you can remove the \b from the regular expression and then it will find all the commas in the above
Friday, June 19, 2020 4:49 PM
Maybe install Visual Studio 2019 (e.g. the Community version) and use the “Format Document” command, that adds spaces after ‘,’ etc., according to options.