简体   繁体   English

用正则表达式提取子字符串

[英]Extract substring with Regex

im trying to extract a substring with regex but im having some troubles... 我试图用正则表达式提取一个子串,但是我遇到了一些麻烦...

The string is build from a columns of strings and i need the the 4th column only 字符串是从一列字符串构建的,我只需要第四列

string stringToExtractFrom = "289  120 00001110 ?? 
4Control@SimApi@@QAEAAV01@ABV01@@Z = ??4Control@SimApi@@QAEAAV01@ABV01@@Z 
(public: class SimApi::Control & __thiscall SimApi::Control::operator=(class 
SimApi::Control const &))"
string pattern = @"\s+\d+\s+\d+\s+\S+\s(.*)\=";
RegexOptions options = RegexOptions.Multiline;

Regex regX = new Regex(pattern, options);
Match m = regX.Match(stringToExtractFrom);

while (m.Success)
{                       

 Group g = m.Groups[1];
 defData += g+"\n";
 m = m.NextMatch();
}

this is the wanted string: ?? 这是通缉的字符串: 4Control@SimApi@@QAEAAV01@ABV01@@Z 4控制@ SimApi @@ QAEAAV01 @ ABV01 @@ž

with the string below it worked when i got the substring i want as a group 当我得到我想要的子字符串时,它下面的字符串有效

1 0 00002E00 ??0ADOFactory@SimApiEx@@QAE@ABV01@@Z = ??0ADOFactory@SimApiEx@@QAE@ABV01@@Z (public: __thiscall SimApiEx::ADOFactory::ADOFactory(class SimApiEx::ADOFactory const &)) 1 0 00002E00 0ADOFactory @ SimApiEx @@ QAE @ ABV01 @@ Z = 0ADOFactory @ SimApiEx @@ QAE @ ABV01 @@ Z(公共:__thiscall SimApiEx :: ADOFactory :: ADOFactory(SimApiEx :: ADOFactory const&) )

If the second string works for you and the first one does not, you might first match 1+ digits and use \\S+ for the third part. 如果第二个字符串对您有用,而第一个不起作用,则您可能首先匹配1个以上的数字,并在第三部分使用\\S+ Then use a negated character class to capture matching not an equals sign: 然后使用否定的字符类捕获不等号的匹配项:

\d+\s+\d+\s+\S+\s+([^=]+) =

See a .NET regex demo | 查看.NET regex演示 | C# Demo C#示范

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

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