我在解析器中使用正则表达式,但是,它似乎给出了一个结果,这是我的代码:正则表达式:
self.seatSelectRegex = [NSRegularExpression regularExpressionWithPattern:@"Seat ([0-9]{1,2}): (.*) \\([$£€]?([0-9.]+) in chips\\).*$" options:NSRegularExpressionAnchorsMatchLines error:&error];
Run Code Online (Sandbox Code Playgroud)
码:
NSMutableDictionary *players = [[NSMutableDictionary alloc] init];
[self.seatSelectRegex enumerateMatchesInString:input options:NSMatchingCompleted range:NSMakeRange(0, input.length) usingBlock:
^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
NSLog(@"%lu", result.range.length);
Player *p = [[Player alloc] init];
p.name = [input substringWithRange:[result rangeAtIndex:2]];
p.seatNumber = [input substringWithRange:[result rangeAtIndex:1]].intValue;
p.stack = [input substringWithRange:[result rangeAtIndex:3]].doubleValue;
[players setValue:p forKey:p.name];
}];
Run Code Online (Sandbox Code Playgroud)
我期待输入3个结果,然而,我得到4,其中最后一个结果的位置= 0和长度= 0(前三个都是正确的).这是常见的行为吗?我应该检查范围的位置和长度,还是某处出现错误?
对于它的价值,这是我的意见:
PokerStars Hand #81669312371: Hold'em No Limit ($0.01/$0.02 USD) - 2012/06/08 16:57:33 CET [2012/06/08 10:57:33 ET]
Table 'Icarus …
Run Code Online (Sandbox Code Playgroud) 使用Objective-C,我对正则表达式非常环保.我遇到了一些困难.
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b([1-9]+)\\b" options:NSRegularExpressionCaseInsensitive error:®Error];
if (regError) {
NSLog(@"%@",regError.localizedDescription);
}
__block NSString *foundModel = nil;
[regex enumerateMatchesInString:self.model options:kNilOptions range:NSMakeRange(0, [self.model length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
foundModel = [self.model substringWithRange:[match rangeAtIndex:0]];
*stop = YES;
}];
Run Code Online (Sandbox Code Playgroud)
我要做的就是拿一个字符串
150A
Run Code Online (Sandbox Code Playgroud)
得到
150
Run Code Online (Sandbox Code Playgroud) 我的目标是计算以多个字母的指定前缀开头的单词数(以字符串形式).案例是以"非"开头的单词.所以在这个例子中......
NSString * theFullTestString = @"nonsense non-issue anonymous controlWord";
Run Code Online (Sandbox Code Playgroud)
......我想,但不是"匿名"或"控制字""胡说"和"不是问题的问题"命中.我的点击总数应为2.
所以这是我的测试代码似乎很接近,但我试过的正则表达式形式都没有正常工作.此代码捕获"废话"(正确)和"匿名"(错误)但不"非问题"(错误).它的数量是2,但出于错误的原因.
NSUInteger countOfNons = 0;
NSString * theFullTestString = @"nonsense non-issue anonymous controlWord";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"non(\\w+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:theFullTestString options:0 range:NSMakeRange(0, theFullTestString.length)];
for (NSTextCheckingResult *match in matches) {
NSRange wordRange = [match rangeAtIndex:1];
NSString* word = [theFullTestString substringWithRange:wordRange];
++countOfNons;
NSLog(@"Found word:%@ countOfNons:%d", word, countOfNons);
}
Run Code Online (Sandbox Code Playgroud)
我很难过.
我想在目标C中编写一个url提取函数.输入文本可以是任何内容,可能包含也可能不包含html锚标记.
考虑一下:
NSString* input1 = @"This is cool site <a href="https://abc.com/coolstuff"> Have fun exploring </a>";
NSString* input2 = @"This is cool site <a target="_blank" href="https://abc.com/coolstuff"> Must visit </a>";
NSString* input3 = @"This is cool site <a href="https://abc.com/coolstuff" target="_blank" > Try now </a>";
Run Code Online (Sandbox Code Playgroud)
我想修改字符串为 "This is cool site https://abc.com/coolstuff
忽略锚标记之间的所有文本.并且需要考虑其他属性,例如锚标记中的_target
我可以做点什么
static NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<a\shref=\"(.*?)\">.*?</a>" options:NSRegularExpressionCaseInsensitive error:nil];;
NSString* modifiedString = [regex stringByReplacingMatchesInString:inputString options:0 range:NSMakeRange(0, [inputString length]) withTemplate:@"$1"];
Run Code Online (Sandbox Code Playgroud)
使用input1工作正常但在其他情况下失败.
谢谢
我有字符串:
Simple text with spaces
Run Code Online (Sandbox Code Playgroud)
我需要选择的正则表达式:
例子:
_ - space
_Simple text __with ___spaces_
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一个正则表达式模式解析字符串.
这是模式:
(\")(.+)(\")\s*(\{)
Run Code Online (Sandbox Code Playgroud)
这是要解析的文本:
"base" {
Run Code Online (Sandbox Code Playgroud)
我想找到这4个捕获组:
1. "
2. base
3. "
4. {
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码尝试捕获这些组
class func matchesInCapturingGroups(text: String, pattern: String) -> [String] {
var results = [String]()
let textRange = NSMakeRange(0, count(text))
var index = 0
if let matches = regexp(pattern)?.matchesInString(text, options: NSMatchingOptions.ReportCompletion, range: textRange) as? [NSTextCheckingResult] {
for match in matches {
// this match = <NSExtendedRegularExpressionCheckingResult: 0x7fac3b601fd0>{0, 8}{<NSRegularExpression: 0x7fac3b70b5b0> (")(.+)(")\s*(\{) 0x1}
results.append(self.substring(text, range: match.range))
}
}
return results
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它只能找到一个范围(0, 8)
等于的组:"base" {
.所以它找到一个组,它是整个字符串而不是4个组. …
基本上我有一个Youtube URL作为字符串,我想从该URL中提取视频ID.我发现目标c中的一些代码如下:
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"?.*v=([^&]+)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:youtubeURL
options:0
range:NSMakeRange(0, [youtubeURL length])];
if (match) {
NSRange videoIDRange = [match rangeAtIndex:1];
NSString *substringForFirstMatch = [youtubeURL substringWithRange:videoIDRange];
}
Run Code Online (Sandbox Code Playgroud)
当我将此代码转换为swift3时,它是:
var error: Error? = nil
var regex = try! NSRegularExpression(pattern: "?.*v=([^&]+)", options: .caseInsensitive)
var match = regex!.firstMatch(in: youtubeURL, options: [], range: NSRange(location: 0, length: youtubeURL.length))!
if match {
var videoIDRange = match.rangeAt(1)
var substringForFirstMatch = (youtubeURL as NSString).substring(with: videoIDRange)
}
Run Code Online (Sandbox Code Playgroud)
给出错误:
致命错误:'试试!' 表达式意外地引发了错误:错误Domain …
在 VS Code 查找/替换编辑器小部件中,我使用命名捕获组(?<end>.*\s*)
。然后我${end}
在替换中使用,但它只是将文字文本而不是捕获的内容放在那里。未命名的捕获组正在按预期工作。
我的正则表达式在 Visual Studio 2019 中运行良好,但我不确定如何调整 VS 代码的命名捕获组语法。
我试图在一组文本中搜索带有括号的所有单词,并在iOS中将其更改为斜体.我正在使用此代码在文本中搜索括号:
static inline NSRegularExpression * ParenthesisRegularExpression() {
static NSRegularExpression *_parenthesisRegularExpression = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_parenthesisRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"\\[[^\\(\\)]+\\]" options:NSRegularExpressionCaseInsensitive error:nil];
});
return _parenthesisRegularExpression;
}
Run Code Online (Sandbox Code Playgroud)
我用它来向我展示比赛:
NSRange matchRange = [result rangeAtIndex:0];
NSString *matchString = [[self.markerPointInfoDictionary objectForKey:@"long_description"] substringWithRange:matchRange];
NSLog(@"%@", matchString);
Run Code Online (Sandbox Code Playgroud)
但它正在返回文本组中第一个[到最后一个]的所有文本.两者之间有很多括号.
我使用此代码将文本更改为斜体:
-(TTTAttributedLabel*)setItalicTextForLabel:(TTTAttributedLabel*)attributedLabel fontSize:(float)Size
{
[attributedLabel setText:[self.markerPointInfoDictionary objectForKey:@"long_description"] afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString)
{
NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
NSRegularExpression *regexp = ParenthesisRegularExpression();
UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:Size];
CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
[regexp enumerateMatchesInString:[mutableAttributedString string] options:0 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 MacOS 上快速解析一个小型项目的 Localizable.string 文件。我只想检索文件中的所有键和值以将它们分类到字典中。
为此,我在NSRegularExpression
可可类中使用了正则表达式。
这是这些文件的样子:
"key 1" = "Value 1";
"key 2" = "Value 2";
"key 3" = "Value 3";
Run Code Online (Sandbox Code Playgroud)
这是我的代码,它应该从加载到 a 的文件中获取键和值String
:
static func getDictionaryFormText(text: String) -> [String: String] {
var dict: [String : String] = [:]
let exp = "\"(.*)\"[ ]*=[ ]*\"(.*)\";"
for line in text.components(separatedBy: "\n") {
let match = self.matches(for: exp, in: line)
// Following line can be uncommented when working
//dict[match[0]] = match[1]
print("(\(match.count)) matches = \(match)")
}
return …
Run Code Online (Sandbox Code Playgroud)