正则表达式;在一个字符串中两个特定的单词之间查找一个单词

时间:2022-04-11 20:13:03

I have the following string:

我有以下字符串:

Account has been reset and assigned the following temporary password:

NvSYVZeg

Please log into your account using your email address and the temporary password.

What I want to do is grab the temporary password (the NvSYVZeg part) and put it into it's own string variable. I've looked for hours and tried multiple expressions that I've found online (tested them at RegexStorm.com & RegExr.com) and they never seem to locate the temp password. I tried:

我要做的是获取临时密码(NvSYVZeg部分)并将其放入自己的字符串变量中。我找了好几个小时,尝试了我在网上找到的多个表达式(在RegexStorm.com和RegExr.com上测试过),但它们似乎从未找到临时密码。我试着:

(?<=password:\s)(.*?)(?=\sPlease) 

and

(password:)(.*?)(Please)

But neither of them worked for me. Any ideas?

但他们两个都不适合我。什么好主意吗?

2 个解决方案

#1


2  

You have more than 1 whitespace in between the password and leading/trailing delimiters. Thus, you need to use \s* (any 0 or more whitespace symbols):

在密码和前导/尾分隔符之间有超过1个空格。因此,您需要使用\s*(任何0或以上空格符号):

password:\s*(.*?)\s*Please
         ^^^     ^^^

or

(?<=password:\s*).*?(?=\s*Please)
             ^^^       ^^^

See the regex demo.

查看演示正则表达式。

The first one might be used like

第一个可以像这样使用

var m = Regex.Match(s, @"password:\s*(.*?)\s*Please");
if (m.Success) 
{
    Console.WriteLine(m.Groups[1].Value);
}

If you use the second one, just access m.Value.

如果您使用第二个,只需访问m.Value。

#2


0  

By using the regex option of RightToLeft we tell the parser to work backwards instead of forwards which in my mind makes it easier to craft a pattern. Note that though the parser works in reverse our pattern is still represented as a forward manner as usual.

通过使用RightToLeft的regex选项,我们告诉解析器可以向后工作,而不是在我的脑海中,使它更容易形成一个模式。请注意,尽管解析器是反向工作的,但我们的模式仍然像往常一样以正向方式表示。


If we mentally, in our pattern, work backwards from an anchor ^Password we just have to eat all spaces by specifying \s+. Then all we have to do once all spaces are consumed is to extract all until a newline. The pattern would be

如果我们精神上,在我们的模式,从一个锚^密码我们只需要通过指定\ s +吃所有空格。然后,一旦所有空格都被占用,我们要做的就是提取所有空格,直到换行。该模式将

 (?<Password>[^\r\n]+)\s+^Please 

Then extract the actual password in C# by using the named group myMatch.Groups["Password"].value.

然后使用命名的组myMatch.Groups[“password”].value提取c#中的实际密码。

Note with the ^ in the pattern we will also need the option Multiline as well.

注意的^模式我们还需要选择多行。

#1


2  

You have more than 1 whitespace in between the password and leading/trailing delimiters. Thus, you need to use \s* (any 0 or more whitespace symbols):

在密码和前导/尾分隔符之间有超过1个空格。因此,您需要使用\s*(任何0或以上空格符号):

password:\s*(.*?)\s*Please
         ^^^     ^^^

or

(?<=password:\s*).*?(?=\s*Please)
             ^^^       ^^^

See the regex demo.

查看演示正则表达式。

The first one might be used like

第一个可以像这样使用

var m = Regex.Match(s, @"password:\s*(.*?)\s*Please");
if (m.Success) 
{
    Console.WriteLine(m.Groups[1].Value);
}

If you use the second one, just access m.Value.

如果您使用第二个,只需访问m.Value。

#2


0  

By using the regex option of RightToLeft we tell the parser to work backwards instead of forwards which in my mind makes it easier to craft a pattern. Note that though the parser works in reverse our pattern is still represented as a forward manner as usual.

通过使用RightToLeft的regex选项,我们告诉解析器可以向后工作,而不是在我的脑海中,使它更容易形成一个模式。请注意,尽管解析器是反向工作的,但我们的模式仍然像往常一样以正向方式表示。


If we mentally, in our pattern, work backwards from an anchor ^Password we just have to eat all spaces by specifying \s+. Then all we have to do once all spaces are consumed is to extract all until a newline. The pattern would be

如果我们精神上,在我们的模式,从一个锚^密码我们只需要通过指定\ s +吃所有空格。然后,一旦所有空格都被占用,我们要做的就是提取所有空格,直到换行。该模式将

 (?<Password>[^\r\n]+)\s+^Please 

Then extract the actual password in C# by using the named group myMatch.Groups["Password"].value.

然后使用命名的组myMatch.Groups[“password”].value提取c#中的实际密码。

Note with the ^ in the pattern we will also need the option Multiline as well.

注意的^模式我们还需要选择多行。