1

Hi I am really struggling with this problem;

I am trying to use regex to match the start and end of a pattern where the middle of the pattern can change. So I would like to use a regex expression that will match the start which is constant and the end which is constant can anyone help?

For instance the start of the pattern is "/>" and ends with "abc"

I am using c#.

Thanks

4

4 回答 4

4
^abc(.*)xyz$

将匹配以 abc 开头并以 xyz 结尾的所有内容。

于 2011-03-02T11:10:53.397 回答
2

有几种选择:

假设您有以下文字:

<foo/> hello first abc hello second abc <bar/> hello third abc

我将展示三个正则表达式,以及它们将匹配的内容:

/\/>.*abc/

将匹配:

/> hello first abc hello second abc <bar/> hello third abc

与其匹配贪心,还可以尽量少取:

/\/>.*?abc/

这将匹配

/> hello first abc
/> hello third abc

但也许你想匹配贪婪,但不是在两者之间打开标签时,你可以这样做:

 /\/>[^<]*abc/

这将匹配

/> hello first abc hello second abc
/> hello third abc

如果你更清楚地指定你想要什么,我可以更清楚地指定我的答案;)

于 2011-03-02T12:40:57.147 回答
0

您要查找的关键项目是匹配行首的 ^ 和匹配行尾的 $。您可以在模式中使用这些来检查字符串开头或结尾的特定字符。

于 2011-03-02T11:21:10.977 回答
0
^\/\u003E.*?abc$. 

这将匹配以“/>”开头并以“abc”结尾的字符串,例如“/>xyzabc”

于 2013-12-10T12:45:54.970 回答