[英]Regex Pattern Matching Concatenationn
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
.用例是我必须用 razor 中的StringBuilder
替换所有字符串连接,并在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.我尝试使用 VS 2013 的查找工具并使用字符+
搜索,我得到了很多行。 Please help me the reg ex pattern for above 2 styles of code lines, so at least number of lines will reduce.请帮我上面2种代码行的reg ex模式,所以至少行数会减少。 I am finding it difficult to get Reg Ex for above 2 styles of code.我发现很难为上述 2 种代码风格获得 Reg Ex。
([\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 https://regex101.com/r/mE9zO7/2
也许是这样的:
(\w+ = \w+\+\w)|(\w+\s?\+=\s?\w+)
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.请记住变量名称可以以_
或[A-Za-z]
开头的变量命名约定,此正则表达式匹配所有此类连接实例。
Regex: ([A-Za-z_]\\w+)\\s*\\+?=\\s*(\\1\\s*)?(\\+?\\s*(([A-Za-z_]\\w+)|"?.*?"?))*;
正则表达式: ([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. ([A-Za-z_]\\w+)
匹配以_
或[A-Za-z]
开头后跟word
的任何字符的变量名称。
\\s*\\+?
Optional whitespace
and +
to match shorthand concatenation operator .可选的whitespace
和+
以匹配速记连接运算符。
\\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. (\\+?\\s*(([A-Za-z_]\\w+)|"?.*?"?))*
必须存在的后继变量名。 These could be zero or many .这些可能是零或许多。 This could also be a literal string like "hello world"
and also matches HTML data .这也可以是像"hello world"
这样的文字字符串,也可以匹配HTML data 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.