匹配单词后的单词

发布于 2024-12-20 18:08:55 字数 174 浏览 0 评论 0原文

我想知道,是否可以使用正则表达式来匹配特定单词后面的单词?例如,假设你有一个这样的字符串:

test abcde  
dummy test fghij

是否可以获得一个包含 test 之后的单词(abcde 和 fghij)的字符串数组?如果是这样,你能解释一下吗?

I'm wondering, is it possible to use regex to match a word after a specific word? For example, suppose you have a string like this:

test abcde  
dummy test fghij

Would it be possible to obtain a string array which contains the words after test (abcde and fghij)? If so, can you explain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

辞别 2024-12-27 18:08:55

这是我给您的建议:

(?:test\s)(?<word>\b\S+\b)

说明

(?:test\s)       matches "test" with a trailing blank, but does not include it in the capture
(?<word>\b\S+\b) matches a word with any character except a whitespace. It will be stored in a group called "word".

这是一个如何使用它的小示例:

var regex = new Regex(@"(?:test\s)(?<word>\b\S+\b)");
var matchCollection = regex.Matches("test abcde   dummy test fghij ");

foreach (Match match in matchCollection)
{
    Debug.WriteLine(match.Groups["word"].Value);
}

Here is my suggestion for you:

(?:test\s)(?<word>\b\S+\b)

Explanation

(?:test\s)       matches "test" with a trailing blank, but does not include it in the capture
(?<word>\b\S+\b) matches a word with any character except a whitespace. It will be stored in a group called "word".

Here is a little example how to use it:

var regex = new Regex(@"(?:test\s)(?<word>\b\S+\b)");
var matchCollection = regex.Matches("test abcde   dummy test fghij ");

foreach (Match match in matchCollection)
{
    Debug.WriteLine(match.Groups["word"].Value);
}
独闯女儿国 2024-12-27 18:08:55

不要使用 RegEx,而是使用简单的 yourString.Split(' ');

然后,您可以:

var strings = yourString.Split(' ');

var afterTest = strings.Skip(strings.IndexOf("test"))

它将更快、更简单、更安全。

另请阅读 Jeff Atwood 关于正则表达式的条目

Do not use RegEx, but a simple yourString.Split(' ');.

Then, you can :

var strings = yourString.Split(' ');

var afterTest = strings.Skip(strings.IndexOf("test"))

It will be faster, simpler and more secure.

Also read Jeff Atwood's entry about regex

新人笑 2024-12-27 18:08:55

您可以使用带有模式 (?<=test).+ 的正向回顾。

var matches = Regex.Matches("test abcd\ndummy test fghij", @"(?<=test).+");

You can use a positive lookbehind with the pattern (?<=test).+.

var matches = Regex.Matches("test abcd\ndummy test fghij", @"(?<=test).+");
甜宝宝 2024-12-27 18:08:55
ArrayList aWrods = new ArrayList();

string[] sArr = yourString.Split(' ');
for (int i=0; i<sArr.Count()-1; i++)
    if (sArr[i] == "test")
        aWords.Add(sArr[i+1]);
ArrayList aWrods = new ArrayList();

string[] sArr = yourString.Split(' ');
for (int i=0; i<sArr.Count()-1; i++)
    if (sArr[i] == "test")
        aWords.Add(sArr[i+1]);

也许这个答案可以帮助

如果你想要抓取从第一个单词之前的空格到单词之后的空格的所有内容使用:

(?:\S+\s)?\S*text\S*(?:\s\S+)?

一个简单的测试:

string input = @"
    This is some dummy text to find a word in a string full with text and words
    Text is too read
    Read my text.
    This is a text-field example
    this is some dummy [email protected] to read";

var matches = Regex.Matches(
    input,
    @"(?:\S+\s)?\S*text\S*(?:\s\S+)?",
    RegexOptions.IgnoreCase
);

匹配是:

dummy text to
with text and
Text is
my text.
a text-field example
dummy [email protected] to

Maybe this answer can help

If you want to grab all the content from the space before first word to the space after the word use:

(?:\S+\s)?\S*text\S*(?:\s\S+)?

A simple tests:

string input = @"
    This is some dummy text to find a word in a string full with text and words
    Text is too read
    Read my text.
    This is a text-field example
    this is some dummy [email protected] to read";

var matches = Regex.Matches(
    input,
    @"(?:\S+\s)?\S*text\S*(?:\s\S+)?",
    RegexOptions.IgnoreCase
);

the matches are:

dummy text to
with text and
Text is
my text.
a text-field example
dummy [email protected] to
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文