需要帮助格式化文本/搜索字符串以查找不同大小的数字
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否完全清楚这里的目标,但解决您发布的特定问题的一种方法是将字符串分解为子字符串,NSString 类使这变得相当容易。下面是一个示例:
另一种方法是使用
NSScanner
的实例来扫描您感兴趣的文本位。这有点复杂,并且需要对 C 和 Objective 有更多的了解-C,但这里是:编辑
顺便说一句,您可以使用 NSPredicate 实例过滤行数组,如下所示:
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:
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:EDIT
By the way, you could filter the array of lines using an instance of NSPredicate as follows:
尽管您可以使用原生 NSString 方法来扫描字符串、使用子字符串以及所有这些爵士乐,但这似乎确实是使用正则表达式的好地方。看起来您将对这些数据进行大量解析,并且由于扫描/子字符串工作将是一个巨大的痛苦。这是一个正则表达式,仅匹配您正在谈论的行,并为您提供两个捕获:
第一个捕获是发布大盲注的人的用户名(我知道您没有要求这样做,但我敢打赌会有帮助的),第二个捕获是数字。较新版本的 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:
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.