如何使用正则表达式获取c#中的部分字符串

时间:2022-09-02 09:58:05

I am try use

我试试用

(?<key>.{5}keyword.{5}?)

to test "other string keyword other text"

测试“其他字符串关键字其他文本”

it will get "ring keyword othe"

它将获得“ring keyword othe”

and I want "akeyword other text" or "other string keyworda" or "akeywordc" are match, too.

我想要“akeyword其他文字”或“其他字符串keyworda”或“akeywordc”也是匹配的。

How to modify regex?

如何修改正则表达式?


I have a long string and I want get find keyword and get it with perfix and suffix string

我有一个很长的字符串,我想获得find关键字,并使用perfix和后缀字符串获取它

demands 5 just a sample it may change to any number like 50

要求5只是一个样本它可能会变成任何数字,如50

my question is if the keyword's position less than 5 and it's not match.

我的问题是关键字的位置是否小于5而且不匹配。

how to get perfix or suffix string with keyword when perfix or suffix string length is

当perfix或后缀字符串长度为时,如何使用关键字获取perfix或后缀字符串

unknow.


Sorry, my question is not clear.

对不起,我的问题不明确。

I want get get perfix or suffix string with keyword. and I want get prefix or sufiix string at most 5 word.

我想用关键字获取perfix或后缀字符串。我希望得到前缀或sufiix字符串最多5个字。

example:

"abcde keyword abcde" I want get "bcde keyword abcd"

“abcde keyword abcde”我想得到“bcde keyword abcd”

and when prefix string less then 5 word

当前缀字符串少于5个字时

"a keyword abcde" I want get "a keyword abcd"

“关键字abcde”我想得到“关键字abcd”

or suffix string less then 5 word

或后缀字符串少于5个字

"abcd keyword a" I want get "abcd keyword a"

“abcd关键字a”我想得到“abcd关键字a”

4 个解决方案

#1


Could this be what you are looking for:

这可能是你想要的:

(?<key>.{0,5}keyword[\s\S]{0,5})

#2


It isn't clear what you want to pass and fail; in particular, the .{5} demands 5 characters before the keyword, so I'm not sure how "akeywordc" should match. The [\s\S]{5} says "5 white-space or non-whitespace"... so again, that demands 5 characters after (and could probably be .{5}).

目前尚不清楚你想要通过和失败;特别是。{5}要求关键字前有5个字符,所以我不确定“akeywordc”应该如何匹配。 [\ s \ S] {5}表示“5个空白或非空白”......所以再次要求5个字符(可能是。{5})。

So: how do you want it to behave?

所以:你希望它如何表现?

For example, (?<key>.{1,5}keyword.{1,5}) will match 1-thru-5 characters before or after (greedily).

例如,(? 。{1,5}关键字。{1,5})将在(贪婪)之前或之后匹配1到5个字符。

#3


This?

(?<key>.{0,5}keyword.{0,5})

You could use something like The Regulator to hack about while you learn how to solve this problem; teach a man to fish, and all that.

当你学习如何解决这个问题时,你可以使用像The Regulator这样的东西来破解;教一个人钓鱼,以及所有这一切。

#4


This could be as simple as (.*?)keyword(.*?) or it could be (.{0,5}) based as per Sebastiens answer, it still depends on what you mean by "unknown".

这可以像(。*?)关键字(。*?)一样简单,也可以根据Sebastiens的回答(。{0,5}),它仍然取决于你所说的“未知”。

Do you mean that any number of matches would be acceptable, or only a specific number of matches but you don't know that number until runtime - in which case how do you know that number at runtime (we can't answer that for you).

你的意思是任何数量的匹配都是可以接受的,或者只是特定数量的匹配,但你不知道这个数字直到运行时 - 在这种情况下你怎么知道这个数字在运行时(我们无法回答你的问题) )。

It would still be best if you posted a set of matching sample strings and non-matching sample strings.

如果您发布了一组匹配的样本字符串和不匹配的样本字符串,那将是最好的。


edit from OP update 2: then it seems you need a combination of mark and sebastiens answers

从OP更新2编辑:那么你似乎需要mark和sebastiens答案的组合

(?<key>.{0,5}keyword.{0,5})

that will include 0 to 5 chars of prefix and 0 to 5 chars of suffix with the match

这将包括匹配的0到5个前缀字符和0到5个后缀字符

#1


Could this be what you are looking for:

这可能是你想要的:

(?<key>.{0,5}keyword[\s\S]{0,5})

#2


It isn't clear what you want to pass and fail; in particular, the .{5} demands 5 characters before the keyword, so I'm not sure how "akeywordc" should match. The [\s\S]{5} says "5 white-space or non-whitespace"... so again, that demands 5 characters after (and could probably be .{5}).

目前尚不清楚你想要通过和失败;特别是。{5}要求关键字前有5个字符,所以我不确定“akeywordc”应该如何匹配。 [\ s \ S] {5}表示“5个空白或非空白”......所以再次要求5个字符(可能是。{5})。

So: how do you want it to behave?

所以:你希望它如何表现?

For example, (?<key>.{1,5}keyword.{1,5}) will match 1-thru-5 characters before or after (greedily).

例如,(? 。{1,5}关键字。{1,5})将在(贪婪)之前或之后匹配1到5个字符。

#3


This?

(?<key>.{0,5}keyword.{0,5})

You could use something like The Regulator to hack about while you learn how to solve this problem; teach a man to fish, and all that.

当你学习如何解决这个问题时,你可以使用像The Regulator这样的东西来破解;教一个人钓鱼,以及所有这一切。

#4


This could be as simple as (.*?)keyword(.*?) or it could be (.{0,5}) based as per Sebastiens answer, it still depends on what you mean by "unknown".

这可以像(。*?)关键字(。*?)一样简单,也可以根据Sebastiens的回答(。{0,5}),它仍然取决于你所说的“未知”。

Do you mean that any number of matches would be acceptable, or only a specific number of matches but you don't know that number until runtime - in which case how do you know that number at runtime (we can't answer that for you).

你的意思是任何数量的匹配都是可以接受的,或者只是特定数量的匹配,但你不知道这个数字直到运行时 - 在这种情况下你怎么知道这个数字在运行时(我们无法回答你的问题) )。

It would still be best if you posted a set of matching sample strings and non-matching sample strings.

如果您发布了一组匹配的样本字符串和不匹配的样本字符串,那将是最好的。


edit from OP update 2: then it seems you need a combination of mark and sebastiens answers

从OP更新2编辑:那么你似乎需要mark和sebastiens答案的组合

(?<key>.{0,5}keyword.{0,5})

that will include 0 to 5 chars of prefix and 0 to 5 chars of suffix with the match

这将包括匹配的0到5个前缀字符和0到5个后缀字符