[英]RegEx find string in string
May I use a RegEx (insted of Substring) in order to get a string in a string? 我可以使用RegEx(由Substring插入)来获取字符串中的字符串吗?
I'd like to get just the table names from a series of INSERT statements 我只想从一系列INSERT语句中获取表名
INSERT INTO tableA VALUES (col1, col2, col3);
INSERT INTO tableB VALUES (col1, col2, col3);
INSERT INTO tableC VALUES (col1, col2, col3);
Using a regEx I would like to get (single line as I'm reading from a file): 使用regEx我想得到(单行,因为我正在从文件中读取):
tableA
tableB
tableC
I've tried with this expression (INTO )([a-z_])*
which gives me 'INTO tableA' which I can use a SubString or Replace to give me the rest, but I'm guessing this may be done in RegEx. 我已经尝试过使用此表达式(INTO )([a-z_])*
,该表达式为我提供了“ INTO tableA”,我可以使用SubString或Replace来给我其余部分,但是我猜想这可以在RegEx中完成。
Use this regex with lookbehind: 将此正则表达式与lookbehind一起使用:
(?i)(?<=into\s+)\S+
var tables = Regex.Matches(s, @"(?i)(?<=into\s+)\S+")
.Cast<Match>().Select(m => m.Value);
Since you are using C#, I will specify how I would do it from start to finish: 由于您使用的是C#,因此我将指定从头到尾的操作方式:
//create regex - the (.*?) is a capture group
var regex = new Regex("INSERT INTO (.*?) VALUES");
//mimic text lines read from a file
var sqlStrings = new string[] {"INSERT INTO tableA VALUES (col1, col2, col3)", "INSERT INTO tableB VALUES (col1, col2, col3)", "INSERT INTO tableC VALUES (col1, col2, col3)"};
foreach (var line in sqlStrings)
{
//get the first match with the regex we created
var match = regex.Match(line);
//print out the first capture group
Console.WriteLine(match.Groups[1].ToString());
}
This will write out the following: 这将写出以下内容:
tableA
tableB
tableC
Not sure of your exact input format (newlines or not) and exactly how you want to output it, but I hope this helps. 不确定您的确切输入格式(换行符或不换行)以及您想要输出的确切方式,但我希望这会有所帮助。
And yes, this can be done a lot more concise, but for clarity's sake I split it up over multiple lines and methods. 是的,这可以做得更简洁,但为了清楚起见,我将它分成多行和方法。
Use a text editor and Find + Replace as follows: 使用文本编辑器,然后按以下步骤查找和替换:
Find: ^INSERT INTO (.*) VALUES.*
Replace: \1
Be sure to check the Regular Expression
option. 确保检查“ Regular Expression
选项。
This is how my Notepad++ screen looks like and trust me, it has worked. 这就是我的Notepad ++屏幕的样子并相信我,它已经奏效了。
You can capture a substring from the matched string using parantheses: 您可以使用parantheses从匹配的字符串中捕获子字符串:
^ *INSERT\s+INTO\s+(\w+)
From the match results, you can extract first captured group using \\1
or $1
depending on your language. 从匹配结果中,您可以使用\\1
或$1
提取第一个捕获的组,具体取决于您的语言。
*
and \\s+
are to ignore extra whitespaces. *
和\\s+
将忽略额外的空格。
in php 在PHP中
$regex = "/INSERT INTO (.*) VALUES/";
in java 在java中
String regex = "INSERT INTO (.*?) VALUES";
first capture group will hold what you want. 第一个捕获组将保持您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.