需要帮助格式化文本/搜索字符串以查找不同大小的数字

发布于 2024-10-14 20:18:13 字数 955 浏览 7 评论 0原文

我刚刚开始使用 obj-c,虽然我可以在字符串中搜索简单的东西,但我还没有达到可以搜索意外的东西的程度。

我有一段格式化文本,它总是以与此类似的方式出现。我不知道如何将其中的某些部分拉出来。我可以创建一个字符串 if 并找到某些部分,但我不确定这是否是正确的路径。我什至不确定创建字符串是否是最好的方法。

我现在正在寻找的是大盲注,它是下面文本中的最后一个数字。由于玩家名称不同,它并不总是位于统一的角色位置。它也不会总是最后,有一些我没有发布的尾随文本。

我可以搜索“大盲注”,然后移动范围来查找数字,但该数字并不总是统一的,这就是我不知道如何允许的。我现在正在使用 subStringWithRange 来查找我想要的内容。

我可以获得第一个数字的索引,但如何允许总共 2-4 位数字?如果我现在将其设置为 4,则会出现超出范围的错误。

Full Tilt Poker Game #27219594154: $1 + $0.20 Sit & Go (Turbo) (211520821), Table 1 -
20/40 - No Limit Hold'em - 18:52:12 ET - 2011/01/12
Seat 1: Twigee (1,485)
Seat 2: tsaf21 (1,508)
Seat 3: dddewdddew (1,385)
Seat 4: MagMan1 (1,770)
Seat 5: Clyde the Fish (1,430)
Seat 6: Lazaro16 (1,340)
Seat 7: ascualliin (1,285)
Seat 8: aipeter (1,730)
Seat 9: AlligatorLega (1,567)
aipeter posts the small blind of 20
AlligatorLega posts the big blind of 40

我真的很感谢你的帮助!到目前为止,Overflow 的每个人都很棒!

谢谢格雷厄姆

I'm just starting out with obj-c and while I can search for simple things in a string I'm not to the point where I can search for un-expected things.

I've got a piece of formatted text that will always appear in a similar fashion to this. What I don't know is how to pull certain pieces of it out. I can create a string out if and find certain pieces but I'm not sure if that's the right path to be taking. I'm not even sure if creating a string is the best way either.

What I'm searching for right now is the big blind, it's the very last number in the text below. It won't always be at a uniform character location because of the varying player names. It won't always be last either, there is trailing text that i didn't post.

I can search for "big blind" then shift the range to find the numbers but that number won't always be uniform and that is what I don't know how to allow for. I'm using subStringWithRange right now to find what I want.

I can get the index of the first number but how do I allow for 2-4 total digits? If I set it to 4 right now it would give me an out of range error.

Full Tilt Poker Game #27219594154: $1 + $0.20 Sit & Go (Turbo) (211520821), Table 1 -
20/40 - No Limit Hold'em - 18:52:12 ET - 2011/01/12
Seat 1: Twigee (1,485)
Seat 2: tsaf21 (1,508)
Seat 3: dddewdddew (1,385)
Seat 4: MagMan1 (1,770)
Seat 5: Clyde the Fish (1,430)
Seat 6: Lazaro16 (1,340)
Seat 7: ascualliin (1,285)
Seat 8: aipeter (1,730)
Seat 9: AlligatorLega (1,567)
aipeter posts the small blind of 20
AlligatorLega posts the big blind of 40

I really appreciate the help! Everyone at Overflow has been awesome so far!

Thanks

Graham

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔少女心 2024-10-21 20:18:13

我不确定我是否完全清楚这里的目标,但解决您发布的特定问题的一种方法是将字符串分解为子字符串,NSString 类使这变得相当容易。下面是一个示例:

NSString *text = // Create the text string however you're currently creating it.

// Break the string into an array of strings, one for each line.
NSArray *lines = [text componentsSeparatedByString:@"\n"];
// Get the last line.
NSString *lastLine = [lines lastObject];

// Break the line into an array of words.
NSArray *words = [lastLine componentsSeparatedByString:@" "];
// Get the last word.
NSString *lastWord = [words lastObject];

// If you need to convert the string to a numeric type...
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:lastWord];
NSLog(@"%@", number);

另一种方法是使用 NSScanner 的实例来扫描您感兴趣的文本位。这有点复杂,并且需要对 C 和 Objective 有更多的了解-C,但这里是:

// Create an instance of NSScanner containing the text
NSScanner *scanner = [NSScanner scannerWithString:text];

// Scan up to the words preceding the text you're interested in.
[scanner scanUpToString:@"big blind of " intoString:NULL];
// Skip past that part.
[scanner scanString:@"big blind of " intoString:NULL];

NSString *numericString;
// Scan the next substring. Here I'm assuming it's at the end of a line,
// so we're scanning until the newline character. Note that the numeric
// string will be created during the call to this method.
[scanner scanUpToString:@"\n" intoString:&numericString];

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:numericString];
NSLog(@"%@", number);

编辑

顺便说一句,您可以使用 NSPredicate 实例过滤行数组,如下所示:

// Break the string into an array of strings, one for each line.
NSArray *lines = [text componentsSeparatedByString:@"\n"];

// Filter the array of lines down to only those containing a given substring.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'big blind'"];
NSArray *matchingLines = [lines filteredArrayUsingPredicate:predicate];

// Make sure that the array is not nil or empty.
if ([matchingLines count] > 0)
{
    // Get the first item in the array of matching lines.
    NSString *firstMatch = [matchingLines objectAtIndex:0];

    // Now break up the line up into words as before, or use an
    // instance of NSScanner to scan the substring based on its position
    // relative to other substrings.
    NSArray *words = [firstMatch componentsSeparatedByString:@" "];
    NSString *lastWord = [words lastObject];
    NSLog(@"%@", lastWord);
}

I'm not sure I'm completely clear on the goal here, but one way to attack the specific problem you posted would be to break the string into substrings, which the NSString class makes fairly easy. Here's an example:

NSString *text = // Create the text string however you're currently creating it.

// Break the string into an array of strings, one for each line.
NSArray *lines = [text componentsSeparatedByString:@"\n"];
// Get the last line.
NSString *lastLine = [lines lastObject];

// Break the line into an array of words.
NSArray *words = [lastLine componentsSeparatedByString:@" "];
// Get the last word.
NSString *lastWord = [words lastObject];

// If you need to convert the string to a numeric type...
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:lastWord];
NSLog(@"%@", number);

Another approach would be to use an instance of NSScanner to scan the bits of text you're interested in. That's a bit more complex, and takes a little bit more understanding of C and Objective-C, but here goes:

// Create an instance of NSScanner containing the text
NSScanner *scanner = [NSScanner scannerWithString:text];

// Scan up to the words preceding the text you're interested in.
[scanner scanUpToString:@"big blind of " intoString:NULL];
// Skip past that part.
[scanner scanString:@"big blind of " intoString:NULL];

NSString *numericString;
// Scan the next substring. Here I'm assuming it's at the end of a line,
// so we're scanning until the newline character. Note that the numeric
// string will be created during the call to this method.
[scanner scanUpToString:@"\n" intoString:&numericString];

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:numericString];
NSLog(@"%@", number);

EDIT

By the way, you could filter the array of lines using an instance of NSPredicate as follows:

// Break the string into an array of strings, one for each line.
NSArray *lines = [text componentsSeparatedByString:@"\n"];

// Filter the array of lines down to only those containing a given substring.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'big blind'"];
NSArray *matchingLines = [lines filteredArrayUsingPredicate:predicate];

// Make sure that the array is not nil or empty.
if ([matchingLines count] > 0)
{
    // Get the first item in the array of matching lines.
    NSString *firstMatch = [matchingLines objectAtIndex:0];

    // Now break up the line up into words as before, or use an
    // instance of NSScanner to scan the substring based on its position
    // relative to other substrings.
    NSArray *words = [firstMatch componentsSeparatedByString:@" "];
    NSString *lastWord = [words lastObject];
    NSLog(@"%@", lastWord);
}
狼亦尘 2024-10-21 20:18:13

尽管您可以使用原生 NSString 方法来扫描字符串、使用子字符串以及所有这些爵士乐,但这似乎确实是使用正则表达式的好地方。看起来您将对这些数据进行大量解析,并且由于扫描/子字符串工作将是一个巨大的痛苦。这是一个正则表达式,仅匹配您正在谈论的行,并为您提供两个捕获:

([\w]+[^\w]*?) posts the big blind of ([\d]*)

第一个捕获是发布大盲注的人的用户名(我知道您没有要求这样做,但我敢打赌会有帮助的),第二个捕获是数字。较新版本的 iOS 包含 NSRegularExpression 类来使用正则表达式,但如果您需要支持旧软件,我强烈推荐 RegexKitLite 适用于 Mac 和 iOS。

Though you can use the native NSString methods to scan through the string, use substrings, and all of that jazz, this really seems like a good place to use regular expressions. It seems like you're going to be doing a lot of parsing of this data, and because of that scanning/substring work is going to be a gigantic pain. Here is a regular expression that matches only the line you are talking about, and gives you two captures:

([\w]+[^\w]*?) posts the big blind of ([\d]*)

The first capture is the username of the person that posted the big blind (I know you didn't ask for that, but I bet it'll be helpful), and the second capture is the number. Newer versions of iOS contain the NSRegularExpression class to use regexes, but if you need to support older software, I highly recommend RegexKitLite for both Mac and iOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文