Products
GG网络技术分享 2025-03-18 16:15 1
I have written this regex to match translation strings. Everything works fine except it only matches single quotations \'\'
in strings, although I\'ve written several rules to match both single and double quotations.
Here is my regex rule:
(Yii::t\\()(\\\'|\\\")(.*?)(\\\'|\\\")\\,(\\\'|\\\")(.*?)(\\\'|\\\")\\)
as expected (\\\'|\\\")
should match both ones but it doesn\'t.
I have also tried the following rules as well:
(\'|\")([\'\"])
Examples:
successfully matches these:
Yii::t(\'backend\',\'My Profile\')Yii::t(\'backend\',\'Log Out\')
does not match these:
Yii::t(\"backend\", \"Search...\")Yii::t(\"backend\", \'Sounds\')
code i\'m using to for matching regex:
re := regexp.MustCompile(`(Yii::t\\()(\\\'|\\\")(.*?)(\\\'|\\\")\\,(\\\'|\\\")(.*?)(\\\'|\\\")\\)`)matches := re.FindAllString(line, -1)
Update:
The problem was because some strings contained white spaces (not because of quotations).图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我已经编写了此正则表达式以匹配翻译字符串。 一切工作正常,除了只匹配字符串中的单引号\'\'</ code>,尽管我编写了一些规则以匹配单引号和双引号。</ p>
这是我的 正则表达式规则:</ p>
(Yii :: t \\()(\\\'| \\“)(。*?)(\\\'| \\”)\\,(\\\'| \\“)(。*?)(\\\'| \\”)\\)</ code> </ pre>
与预期相同(\\\'| \\“)</ code> 应该同时匹配两个。</ p>
我也尝试了以下规则:</ p>
(\'|“) ([\'“])
</ code> </ pre>
示例:</ p>
成功匹配以下内容:</ p>
Yii :: t(\'backend\',\'My Profile\')Yii :: t(\'backend\',\'Log Out\')
</ code> </ pre>
< p>与以下项不匹配:</ p>
Yii :: t(“ backend”,“ Search ...”)Yii :: t(“ backend”,\'Sounds\' )
</ code> </ pre>
我用来匹配正则表达式的代码:</ p>
re:= regexp.MustCompile(` (Yii的::牛逼\\()(\\ \'| \\“?)(*)(\\\' | \\ ”)\\(\\\'| \\“?)(*)(\\\'| \\”)\\) `)matches:= re.FindAllString(line,-1)
</ code> </ pre>
Update:
问题是因为某些字符串 留有空格(不是因为引号)。</ strong> </ p>
</ div>
网友观点:
Try this Regex:
Yii::t\\((?:[\'\\\"][^\'\\\"]*[\'\\\"],?\\s*)*\\)
Explanation:
Yii::t\\(
- matches Yii::t(
literally(?:[\'\\\"][^\'\\\"]+[\'\\\"],?\\s*)*\\)
[\'\\\"]
- matches either \'
or \\\"
[^\'\\\"]*
- matches 0+ occurrences of any character that is neither \'
nor \\\"
[\'\\\"]
- matches a single occurrence of either \'
or \\\"
,?
- matches 0 or 1 occurrence of a ,
\\s*
- matches 0+ occurrences of a whitespace*
- The last *
matches the above 5 sub-patterns 0+ times\\)
- matches )
literally
Alternative Solution:
Yii::t\\(\\s*[\'\\\"][^\'\\\"]*[\'\\\"]\\s*(?:,\\s*[\'\\\"][^\'\\\"]*[\'\\\"]\\s*)*\\)
This RegEx matches everything:
(Yii::t\\(\\s*)(\\\'|\\\\\")(.*?)(\\\'|\\\\\")\\,\\s*(\\\'|\\\\\")(.*?)(\\\'|\\\\\")\\)
Try this Regex , This matches everything :
Yii::t\\((\'|\\\")(.*)(\\\'|\\\\\"),(\'|[ ](\\\"|\'))(.*)(\'|\\\")\\)
正则表达式字符匹配攻略
正则表达式是匹配模式,要么匹配字符,要么匹配位置。请记住这句话。
然而关于正则如何匹配字符的学习,大部分人都觉得这块比较杂乱。
毕竟元字符太多了,看起来没有系统性,不好记。本文就解决这个问题。
内容包括:
1. 两种模糊匹配
2. 字符组
3. 量词
4. 分支结构
5. 案例分析
1. 两种模糊匹配
如果正则只有精确匹配是没多大意义的,比如/hello/,也只能匹配字符串中的"hello"这个子串。
Demand feedback