Searching for a specific format in a string

淺唱寂寞╮ 提交于 2019-12-11 02:46:06

问题


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).


回答1:


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.

Hope this helps!




回答2:


Like will return the whole string (assuming you're comparing to "*###-##-####*". Apart from that I only know of using regex.

It's really simple though, is the pattern simply a number? Such as 999-99-9999? If so your regex will be:

\d\d\d-\d\d-\d\d\d\d

You can now use the methods which will extract this number (if match found).



来源:https://stackoverflow.com/questions/7014295/searching-for-a-specific-format-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!