简体   繁体   中英

RegEx working in JavaScript but not in C#

I currently have a working WordWrap function in Javascript that uses RegEx. 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. 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. I looked into PCRE.NET but have had no luck with that either.

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#?

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. 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. 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:

"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. Also each language has it's own regex pattern executor, so Regex is language dependent. 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.

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.

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).

You have:

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

when what I believe you want is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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