简体   繁体   English

C# 中的 Regex 模式用于 C# 中的 Regex.Replace()

[英]Regex pattern in C# for Regex.Replace() in C#

I have a string我有一个字符串

string str1 = "SELECT * FROM A INNER JOIN B JOIN C JOIN A INNER JOIN B LEFT JOIN C";

I want to Replace the JOIN not followed by INNER , LEFT etc with \\nJOIN我想用\\nJOIN替换后面没有INNERLEFT等的JOIN

My desired output is我想要的输出是

SELECT * FROM A INNER JOIN B \nJOIN C \nJOIN A INNER JOIN B LEFT JOIN C 

I am a novice C# programmer and what I wanted to use is我是一个新手 C# 程序员,我想使用的是

var pattern = NOT IN("INNER" || "OUTER" || "LEFT") + @"\b"+ "JOIN";
var output = Regex.Replace(str1, pattern, "\nJOIN", RegexOptions.IgnoreCase); 

Here I stuck with implementing the NOT IN("INNER" || "OUTER" || "LEFT") with RegEx.在这里,我坚持使用正则表达式实现NOT IN("INNER" || "OUTER" || "LEFT")

Can you please help me in this regard.你能在这方面帮助我吗? Thanks in advance.提前致谢。

I think this is what you're looking for:我认为这就是你要找的:

string str1 = "SELECT * FROM A INNER JOIN B JOIN C JOIN A INNER JOIN B LEFT JOIN C";
string result = Regex.Replace(str1, @"(?<!INNER)\s+JOIN", "\nJOIN");
//result: SELECT * FROM A INNER JOIN B \nJOIN C \nJOIN A INNER JOIN B LEFT \nJOIN C

I used this handy website to learn how to do it: regex101我使用这个方便的网站来学习如何做: regex101

Remember to use the quick reference search in the lower right to find the character or expression you need (eg I searched "Not" in your case)请记住使用右下角的快速参考搜索来查找您需要的字符或表达式(例如,我在您的情况下搜索了“Not”)

UPDATED: to make it more robust and allow 1 to n amount of blank spaces between INNER and JOIN更新:使其更健壮,并在 INNER 和 JOIN 之间允许 1 到 n 个空格

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

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