标签: nsregularexpression

iOS中的电子邮件验证

可以使用RegexKitLite库(iOS2.0),NSPredicate(iOS 3.0及以上版本)和NSRegularExpression(iOS 4.0)完成iPhone编程中的电子邮件验证检查.但是,任何人都可以说明一个优于另一个的优势,这是三个所述的最佳验证选项.

email validation nspredicate ios nsregularexpression

4
推荐指数
2
解决办法
2万
查看次数

NSRegularExpression,指定区分大小写的匹配?

有没有办法NSRegularExpression指定您想要进行区分大小写的搜索?我试图在下面的文本中匹配大写TAG"ACL".我使用的模式很简单:

// Pattern
[A-Z]+

// SearchText
<td align=\"left\" nowrap><font face=\"courier, monospace\" size=\"-1\">ACL*</font></td>

// Code:
NSString *textBuffer = @"<td align=\"left\" nowrap><font face=\"courier, monospace\" size=\"-1\">ACL*</font></td>";
NSString *pattern = @"([A-Z]+)";
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSTextCheckingResult *result = [regExp firstMatchInString:textBuffer options:0 range:NSMakeRange(0, [textBuffer length])];
NSLog(@"OBJECT CLASS: %@", [textBuffer substringWithRange:[result range]]);
Run Code Online (Sandbox Code Playgroud)

输出:(使用case-Insensative我按预期获得第一个"td",当我真正想要的是"ACL"时

我知道那NSRegularExpressionCaseInsensitive是错的,我希望会有一个NSRegularExpressionCaseSensitive.还有一个flagOption ?(i),它还指定了一个不区分大小写的搜索,但同样没有任何案例敏感的搜索.我错过了什么?

iphone cocoa-touch objective-c ios nsregularexpression

4
推荐指数
1
解决办法
2901
查看次数

iOS电子邮件地址验证

我有一个表单,我想要做的是在将电子邮件地址插入我的数据库之前验证它们.我已经阅读过多篇帖子,但没有一篇能解决我的问题.

这是我到目前为止的代码.

-(BOOL) validEmail:(NSString*) emailString {
    NSString *regExPattern = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";  

    NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];

    NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString options:0 range:NSMakeRange(0, [emailString length])];

    if (regExMatches == 0) {
        return NO;
    } else
        return YES;
}
Run Code Online (Sandbox Code Playgroud)

正则表达式几乎匹配每个电子邮件地址,但是当地址格式为abc,def @ gmail.com时,它无法将其作为无效地址捕获.它不会拿起逗号.

任何人有任何想法,我出错了?谢谢

regex ios nsregularexpression ios6

4
推荐指数
3
解决办法
1万
查看次数

NSRegularExpression行为怪异(正则表达式是正确的)

我有这个正则表达式

([0-9]+)\(([0-9]+),([0-9]+)\)
Run Code Online (Sandbox Code Playgroud)

我正在使用构造一个没有选项(0)的NSRegularExpression.该表达式应匹配字符串

1(135,252)
Run Code Online (Sandbox Code Playgroud)

并且产生三个匹配:1,135,252.现在,我已经通过debuggex.com确认表达式是正确的并且做我想要的.但是,iOS拒绝承认我的努力和以下代码

NSString *nodeString = @"1(135,252)";
NSArray *r = [nodeRegex matchesInString:nodeString options:0 range:NSMakeRange(0, nodeString.length)];
NSLog(@"--- %@", nodeString);
for(NSTextCheckingResult *t in r) {
    for(int i = 0; i < t.numberOfRanges; i++) {
        NSLog(@"%@", [nodeString substringWithRange:[t rangeAtIndex:i]]);
    }
}
Run Code Online (Sandbox Code Playgroud)

坚持说

--- 1(135,252)
135,252
13
5,252
5
252
Run Code Online (Sandbox Code Playgroud)

这显然是错的.

思考?

regex objective-c ios nsregularexpression

4
推荐指数
1
解决办法
558
查看次数

在iOS中搜索带有括号的单词

我试图在一组文本中搜索带有括号的所有单词,并在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)

regex ios nsregularexpression

4
推荐指数
1
解决办法
733
查看次数

NSRegularExpression是否支持部分不区分大小写?

正如标题所述,我想知道在Objective-c或Swift中是否支持部分不区分大小写的NSRegularExpression?

即,模式是否会识别(?ismx)?如果没有,是否有这种无能的简短原因?

我非常感谢你的解释.

regex xcode objective-c nsregularexpression swift

4
推荐指数
1
解决办法
920
查看次数

当源包含unicode字符时,Swift正则表达式匹配失败

我正在尝试使用NSRegularExpression进行简单的正则表达式匹配,但是当源包含多字节字符时,我遇到了一些匹配字符串的问题:

let string = "D 9"

// The following matches (any characters)(SPACE)(numbers)(any characters)
let pattern = "([\\s\\S]*) ([0-9]*)(.*)"

let slen : Int = string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)

var error: NSError? = nil

var regex = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators, error: &error)

var result = regex?.stringByReplacingMatchesInString(string, options: nil, range: NSRange(location:0,
length:slen), withTemplate: "First \"$1\" Second: \"$2\"")
Run Code Online (Sandbox Code Playgroud)

上面的代码按预期返回"D"和"9"

如果我现在更改第一行以包含英国"英镑"货币符号,如下所示:

let string = "£ 9"
Run Code Online (Sandbox Code Playgroud)

然后匹配不起作用,即使([\\s\\S]*)表达式的部分仍然匹配任何前导字符.

我知道£符号将占用两个字节,但通配符前导匹配应该忽略那些不应该吗?

有人能解释一下这里发生了什么吗?

regex nsregularexpression swift xcode6

4
推荐指数
1
解决办法
1216
查看次数

如何使用NSRegularExpression删除括号内的文本?

我尝试删除括号内的部分字符串.

例如,对于字符串"(This should be removed) and only this part should remain",在使用NSRegularExpression之后应该成为"and only this part should remain".

我有这个代码,但没有任何反应.我用RegExr.com测试了我的正则表达式代码,它运行正常.我将不胜感激任何帮助.

NSString *phraseLabelWithBrackets = @"(test text) 1 2 3 text test";
NSError *error = NULL;
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"/\\(([^\\)]+)\\)/g" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *phraseLabelWithoutBrackets = [regexp stringByReplacingMatchesInString:phraseLabelWithBrackets options:0 range:NSMakeRange(0, [phraseLabelWithBrackets length]) withTemplate:@""];
NSLog(phraseLabelWithoutBrackets);
Run Code Online (Sandbox Code Playgroud)

regex objective-c nsregularexpression

4
推荐指数
1
解决办法
244
查看次数

试图在 MacOS 上快速解析一个小项目的 Localizable.string 文件

我正在尝试在 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)

regex string macos nsregularexpression swift

4
推荐指数
1
解决办法
2273
查看次数

如何使用NSRegularExpression解析字符串以使用正则表达式提取数字?

我是正则表达式的新手,并且不知道如何使用它们.我想为下面的文本创建一个正则表达式来获取min,avg和max时间.我将使用NSRegularExpression来获取范围然后获取字符串.

有人可以帮助创建以下文本的正则表达式.

round-trip min/avg/max/stddev = 3.073/6.010/13.312/2.789 ms
Run Code Online (Sandbox Code Playgroud)

regex macos cocoa objective-c nsregularexpression

3
推荐指数
1
解决办法
2693
查看次数