正则表达式匹配开始和结束字符串 [英] Regex matching beginning AND end strings
问题描述
这看起来应该是微不足道的,但我不太擅长正则表达式,而且这对 Google 来说似乎并不容易.
This seems like it should be trivial, but I'm not so good with regular expressions, and this doesn't seem to be easy to Google.
我需要一个以字符串dbo"开头的正则表达式.并以字符串 '_fn' 结尾
I need a regex that starts with the string 'dbo.' and ends with the string '_fn'
就我而言,我不在乎这两个字符串之间是什么字符,只要开头和结尾正确即可.
So far as I am concerned, I don't care what characters are in between these two strings, so long as the beginning and end are correct.
这是为了匹配 SQL 服务器数据库中的函数.
This is to match functions in a SQL server database.
例如:
dbo.functionName_fn - Match
dbo._fn_functionName - No Match
dbo.functionName_fn_blah - No Match
推荐答案
如果您在较大的文本中搜索匹配项,您不想使用 ^
和 $
正如其他一些响应者所说;那些匹配文本的开头和结尾.试试这个:
If you're searching for hits within a larger text, you don't want to use ^
and $
as some other responders have said; those match the beginning and end of the text. Try this instead:
\bdbo\.\w+_fn\b
\b
是一个 词边界:它匹配的位置是前面是一个单词字符而不后面是 1,或者后面是一个单词字符而不前面是 1.此正则表达式将在以下任何字符串中找到您要查找的内容:
\b
is a word boundary: it matches a position that is either preceded by a word character and not followed by one, or followed by a word character and not preceded by one. This regex will find what you're looking for in any of these strings:
dbo.functionName_fn
foo dbo.functionName_fn bar
(dbo.functionName_fn)
...但不是在这个:
foodbo.functionName_fnbar
\w+
匹配一个或多个单词字符"(字母、数字或 _
).如果你需要更多的东西,你可以尝试 \S+
(一个或多个非空白字符)或 .+?
(一个或多个除换行符以外的任何字符,非- 贪婪).非贪婪的 +?
防止它意外匹配 dbo.func1_fn dbo.func2_fn
之类的东西,就好像它只是一次命中一样.
\w+
matches one or more "word characters" (letters, digits, or _
). If you need something more inclusive, you can try \S+
(one or more non-whitespace characters) or .+?
(one or more of any characters except linefeeds, non-greedily). The non-greedy +?
prevents it from accidentally matching something like dbo.func1_fn dbo.func2_fn
as if it were just one hit.
这篇关于正则表达式匹配开始和结束字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!