在字符串中搜索特定格式 [英] Searching for a specific format in a string

查看:43
本文介绍了在字符串中搜索特定格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据数据中是否出现 SSN 号码来累积一些数据.我把它放在一个字符串中,我需要做的是在字符串中搜索一个数字模式,例如:

I need to accum some data based on whether or not an SSN number appears in the data. I have it in a string, what I need to do is search for a numeric pattern within the string such as:

###-##-####

我稍微研究了正则表达式,但我没有太多时间深入阅读(我的公司本周需要该程序),而且在这么短的时间内掌握它似乎太复杂了.到目前为止,我遇到的是:

I looked into regex a bit, but I don't have much time to read deep into it (my company needs the program this week) and it seems too complex to grasp in such a short time. What I've came across so far is:

If (rec.address1 Like "###-##-####")

但我不知道这是否会将它从字符串中过滤掉(它可能会带有附加地址).

but I don't know if this will filter it out of the string (it can appear with an address attached to it).

推荐答案

如果你正在使用 Like,试试这个模式(有一些例子):

If you're using Like, try this pattern (with some examples):

Dim pattern as string = "*###-##-####*"

Console.WriteLine("Dave111-22-3333H" Like pattern)      'True
Console.WriteLine("111-22-3333H" Like pattern)          'True
Console.WriteLine("Dave111-22-3333" Like pattern)       'True
Console.WriteLine("Dave111-2DDD2-3333H" Like pattern)   'False
Console.WriteLine("333111-22-3333" Like pattern)        'True
Console.WriteLine("D111-22-33331231223" Like pattern)   'True

* 是通配符.这是一个链接有一组很好的例子.

The * is a wildcard character. Here's a link with a nice set of examples.

如果您只是在问题中使用该模式,它不会将其过滤掉.但是如果你用通配符包围你的模式,那么它就会.

If you just use the pattern in your question, it won't filter it out. But if you surround your pattern with wildcards, then it will.

希望这会有所帮助!

这篇关于在字符串中搜索特定格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆