2

I am trying to find some patterns of coding style in my solution. The use case is that I have to replace all the string concatenation with StringBuilder in razor and find all string concatenation in JavaScript. I am finding it very difficult to get all such code lines which have string concatenation in the following 2 ways.

1) str = str+str2+str3+"str4 " etc;
2) str+=str2+str3+"str4 " etc;

I tried using finding tool of VS 2013 and searched with character +, I got hell lot of lines. Please help me the reg ex pattern for above 2 styles of code lines, so at least number of lines will reduce. I am finding it difficult to get Reg Ex for above 2 styles of code.

5
  • What's your expected output from those strings ? Commented Mar 24, 2016 at 11:37
  • I dont need any output. Some have done concatenation is style 1 and some using 2nd way. I need to use finding(ctrl+f) functionality of VS 2013 and find such code lines. So i can replace them with stringbuilder. Commented Mar 24, 2016 at 11:39
  • So the variable name could be anything other than str and str2 ? Commented Mar 24, 2016 at 11:46
  • yes. It can be anything but both on LHS and RHS should match. Commented Mar 24, 2016 at 11:48
  • When you use shorthand operator += there could be only one value on right hand side. str+=str2+str3+"str4 " is invalid. Commented Mar 24, 2016 at 12:59

3 Answers 3

2
([\w_]+[\w\d]*)(?:\s*=\s*\1{1}\s*\+\s*|\s*\+=\s*)("?[\w_]+[\w\d]*"?)(\s*\+\s*"?([\w_]+[\w\d]*"?))*;

Here I assumed that variable may start with an underline or a alphabetic character. You may want to make some changes to it if you need.

https://regex101.com/r/mE9zO7/2

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the answer. It is working to some extent. Like it is working for such kind of statements strCloseHTML = strCloseHTML + PagingControlHTML; But not working for the following kind of statements strRowHTML = strRowHTML + "<tr class='gridrow odd action'>";
Got a question, on the link it says 2 matches, 8293 steps. If we have a large input, will performance be impacted? Should poster go for a while(readline()) approach?
ouch you want this to be able to match html code too? that would be lots of trouble doing such a thing with regular expressions. I don't know what you are trying to do. but you are doing it wrong.
still its not matching strRowHTML = strRowHTML + "<tr class='gridrow odd action'>";
Ok Can you please tell me the RegEx for just matching str=str+ and str+=
|
0

Perhaps something like:

(\w+ = \w+\+\w)|(\w+\s?\+=\s?\w+)

Comments

0

Keeping in mind the variable naming conventions that variable name can begin with an _ or [A-Za-z] this regex matches all such instances of concatenation.

Regex: ([A-Za-z_]\w+)\s*\+?=\s*(\1\s*)?(\+?\s*(([A-Za-z_]\w+)|"?.*?"?))*;

Explanation:

  • ([A-Za-z_]\w+) matches the variable name starting with _ or [A-Za-z] followed by any character from word class.

  • \s*\+? Optional whitespace and + to match shorthand concatenation operator.

  • \s*(\1\s*)? If shorthand operator is not used then this will match conventional way of concatenating.

  • (\+?\s*(([A-Za-z_]\w+)|"?.*?"?))* succeeding variable names which must be present. These could be zero or many. This could also be a literal string like "hello world" and also matches HTML data.

Regex101 Demo

3 Comments

Its is not matching any of the following code lines strCloseHTML = strCloseHTML + PagingControlHTML; strRowHTML = strRowHTML + "<tr class='gridrow odd action'>";
@user2998990: You didn't mentioned that you wish to match HTML code too. When you edit a question, add the EDIT title. Otherwise answers which worked for your previous version of question might not work for added parts and people will down vote them as Not useful
I am sorry. But string can be anything in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.