I am trying to use regex to search through a string: "K1B92 (D) [56.094]"
and I want to grab the "(D)"
including the parentheses surrounding the "D"
. I am having trouble finding the correct expression to match the actual parentheses as simply putting parentheses will take it as a block and trying to escape the parentheses with "\"
making it think its an expression to be evaluated. I also tried escaping "\("
with "\\("
as so: "\\([ABCD])\\)"
but without luck. This is the code I have been using:
我正在尝试使用正则表达式搜索字符串:“K1B92(D)[56.094]”并且我想抓住“(D)”,包括围绕“D”的括号。我无法找到正确的表达式来匹配实际的括号,因为简单地将括号放在一个块中并试图用“\”来转义括号,使它认为它是一个要评估的表达式。我也尝试转义“\(”与“\\(”如此:“\\([ABCD])\\)”,但没有运气。这是我一直在使用的代码:
let str = "K1B92 (D) [56.094]"
let regex = NSRegularExpression(pattern: "\\b\\([ABCD])\\)\\b", options: NSRegularExpressionOptions.CaseInsensitive, error: nil)
let match = regex?.firstMatchInString(str, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, count(str)))
let strRange = match?.range
let start = strRange?.location
let length = strRange?.length
let subStr = str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, start!), end: advance(str.startIndex, start! + length!)))
// "\\b\([ABCD])\)\\b" returns range only for the letter "D" without parentheses.
// "\\b\\([ABCD])\\)\\b" returns nil
Can direct me towards the correct expression please? Thank you very much.
能引导我走向正确的表达吗?非常感谢你。
2 个解决方案
#1
5
The
Correction: As @vacawama correctly said in his answer, the parentheses do not match here. \\([ABCD])\\)
part is OK,
\\([ABCD]\\)
matches one of the letters A-D enclosed in parentheses.
\\([ABCD])\\)部分没问题,更正:正如@vacawama在答案中正确说的那样,括号在这里不匹配。 \\([ABCD] \\)匹配括号中的一个字母A-D。
The other problem is that there is no word boundary (\b
pattern) between a space and a parenthesis.
另一个问题是空格和括号之间没有单词边界(\ b模式)。
So you could either (depending on your needs), just remove the \b
patterns, or replace them by \s
for white space:
所以您可以(根据您的需要),只需删除\ b模式,或者用\ s替换它们以获取空白:
let regex = NSRegularExpression(pattern: "\\s\\([ABCD]\\)\\s", ...
But since the matched string should not include the space you need a capture group:
但由于匹配的字符串不应包含空间,因此需要捕获组:
let regex = NSRegularExpression(pattern: "\\s(\\([ABCD]\\))\\s", ...
// ...
let strRange = match?.rangeAtIndex(1)
#2
4
The regular expression you need is "\\([ABCD]\\)"
. You need the double escape \\
before both open paren (
and close paren )
.
您需要的正则表达式是“\\([ABCD] \\)”。在打开paren(和关闭paren)之前你需要双重转义\\。
#1
5
The
Correction: As @vacawama correctly said in his answer, the parentheses do not match here. \\([ABCD])\\)
part is OK,
\\([ABCD]\\)
matches one of the letters A-D enclosed in parentheses.
\\([ABCD])\\)部分没问题,更正:正如@vacawama在答案中正确说的那样,括号在这里不匹配。 \\([ABCD] \\)匹配括号中的一个字母A-D。
The other problem is that there is no word boundary (\b
pattern) between a space and a parenthesis.
另一个问题是空格和括号之间没有单词边界(\ b模式)。
So you could either (depending on your needs), just remove the \b
patterns, or replace them by \s
for white space:
所以您可以(根据您的需要),只需删除\ b模式,或者用\ s替换它们以获取空白:
let regex = NSRegularExpression(pattern: "\\s\\([ABCD]\\)\\s", ...
But since the matched string should not include the space you need a capture group:
但由于匹配的字符串不应包含空间,因此需要捕获组:
let regex = NSRegularExpression(pattern: "\\s(\\([ABCD]\\))\\s", ...
// ...
let strRange = match?.rangeAtIndex(1)
#2
4
The regular expression you need is "\\([ABCD]\\)"
. You need the double escape \\
before both open paren (
and close paren )
.
您需要的正则表达式是“\\([ABCD] \\)”。在打开paren(和关闭paren)之前你需要双重转义\\。