简体   繁体   English

RegEx可在JavaScript中工作,但不能在C#中工作

[英]RegEx working in JavaScript but not in C#

I currently have a working WordWrap function in Javascript that uses RegEx. 我目前在使用RegEx的Javascript中有一个有效的WordWrap函数。 I pass the string I want wrapped and the length I want to begin wrapping the text, and the function returns a new string with newlines inserted at appropriate locations in the string as shown below: 我传递了要包装的字符串和要开始包装文本的长度,然后该函数返回一个新字符串,并在字符串的适当位置插入了换行符,如下所示:

 wordWrap(string, width) { let newString = string.replace( new RegExp(`(?![^\\\\n]{1,${width}}$)([^\\\\n]{1,${width}})\\\\s`, 'g'), '$1\\n' ); return newString; } 

For consistency purposes I won't go into, I need to use an identical or similar RegEx in C#, but I am having trouble successfully replicating the function. 为了保持一致性,我不再赘述,我需要在C#中使用相同或相似的RegEx,但是在成功复制该函数时遇到了麻烦。 I've been through a lot of iterations of this, but this is what I currently have: 我已经经历了很多迭代,但这是我目前拥有的:

        private static string WordWrap(string str, int width)
    {
        Regex rgx = new Regex("(?![^\\n]{ 1,${" + width + "}}$)([^\\n]{1,${" + width + "}})\\s");
        MatchCollection matches = rgx.Matches(str);

        string newString = string.Empty;

        if (matches.Count > 0)
        {
            foreach (Match match in matches)
            {
                newString += match.Value + "\n";
            }
        }

        else
        {
            newString = "No matches found";
        }

        return newString;
    }

This inevitably ends up finding no matches regardless of the string and length I pass. 不管我传递的字符串和长度如何,都不可避免地找不到匹配项。 I've read that the RegEx used in JavaScript is different than the standard RegEx functionality in .NET. 我已经读过JavaScript中使用的RegEx与.NET中的标准RegEx功能不同。 I looked into PCRE.NET but have had no luck with that either. 我调查了PCRE.NET,但也没有运气。

Am I heading in the right general direction with this? 我是否正朝着正确的总体方向前进? Can anyone help me convert the first code block in JavaScript to something moderately close in C#? 谁能帮助我将JavaScript中的第一个代码块转换为C#中程度接近的代码?

edit: For those looking for more clarity on what the working function does and what I am looking for the C# function to do: What I am looking to output is a string that has a newline (\\n) inserted at the width passed to the function. 编辑:对于那些希望更清楚地了解该工作函数的功能以及我正在寻找的C#函数要进行的操作:我想要输出的是一个字符串,该字符串在传递给的宽度上插入了一个换行符(\\ n)。功能。 One thing I forgot to mention (but really isn't related to my issue here) is that the working JavaScript version finds the end of the word so it doesn't cut up the word. 我忘了提及的一件事(但实际上与这里的问题无关)是,有效的JavaScript版本找到了单词的结尾,因此不会分解单词。 So for example this string: 因此,例如此字符串:

"This string is really really long so we want to use the word wrap function to keep it from running off the page.\n"

...would be converted to this with the width set to 20: ...将被转换为20的宽度:

"This string is really \nreally long so we want \nto use the word wrap \nfunction to keep it \nfrom running off the \npage.\n"

Hope that clears it up a bit. 希望这可以清除它。

JavaScript and C# Regex engines are different. JavaScript和C#Regex引擎是不同的。 Also each language has it's own regex pattern executor, so Regex is language dependent. 而且每种语言都有自己的regex模式执行器,因此Regex依赖于语言。 It's not the case, if it is working for one language so it will work for another. 并非如此,如果它适用于一种语言,那么它将适用于另一种语言。

C# supports named groups while JavaScript doesn't support them. C#支持命名组,而JavaScript不支持它们。

So you can find multiple difference between these two languages regex. 因此,您可以找到这两种语言正则表达式之间的多重差异。

There are issues with the way you've translated the regex pattern from a JavaScript string to a C# string. 将正则表达式模式从JavaScript字符串转换为C#字符串的方式存在问题。

You have extra whitespace in the c# version, and you've also left in $ symbols and curly brackets { that are part of the string interpolation syntax in the JavaScript version (they are not part of the actual regex pattern). 在c#版本中,您还有多余的空格,并且在$符号和大括号{中,它们是JavaScript版本中字符串插值语法的一部分(它们不是实际的regex模式的一部分)。

You have: 你有:

"(?![^\\n]{ 1,${" + width + "}}$)([^\\n]{1,${" + width + "}})\\s"

when what I believe you want is: 当我相信你想要的是:

"(?![^\\n]{1," + width + "}$)([^\\n]{1," + width + "})\\s"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM