2

假设我有以下类似的文字:

_startOneEnd
_startTwoEnd
_startThreeEnd

我想匹配:以_start开头, 以End结尾 ,我想捕获中间的位,例如,上面变量中的一、二、三:

谁能建议一个正则表达式来捕捉这个?

4

3 回答 3

4

如果每一行输入只包含与您的示例类似的文本,则应该这样:

/^_start(.*)End$/

^模式锚定到字符串的开头。将其$锚定到字符串的末尾。括号捕获中间部分。

于 2012-12-13T07:52:44.970 回答
1

It isn't clear if the part in the middle may only be the examples given.
If so, use this:

_start((One)|(Two)|(Three))End

If not, is it can be anything, try this:

_start(.*?)End

Note that the match is non-greedy.

于 2012-12-13T07:56:21.550 回答
1

C#中,您可以使用:

(?<=_start).*(?=End)
于 2012-12-13T07:54:36.300 回答