Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex pattern help

Status
Not open for further replies.

ClulessChris

IS-IT--Management
Jan 27, 2003
890
0
0
GB
I’m trying to count the number of personal titles in a body of text. Each title is prefixed with ‘Y00’. I use this regex pattern: (Y00)[A-z]*\s
This works well to match the prefix followed by Alpha characters until whitespace. And so will match:

Y00Mr Y00Miss Y00Prof

However it misses the match if, as is sometimes the case, the tile is followed by ‘.’

How can I amend the pattern to also match :

Y00Mr. Y00Miss. Y00Prof.

Thanks for your time.


Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Ok I seem to have found the answer with (Y00)([A-z]*\s|[A-z]*\.)
however if you have any suggestions to improve this I'd be greatfull for your input.

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
And what about this?
Code:
Y00([A-z]+\.*)
 
If all you are trying to do is count them then you only need to match Y00

Code:
[blue]Public Function CountTitles(strText As String) As Long
    With CreateObject("vbscript.regexp")
        .Pattern = "Y00"
        .Global = True
        CountTitles = .Execute(strText).Count
    End With
End Function[/blue]

If, on the other hand, you need to capture the titles, then I'd simplify your expression to something like

[tt](Y00)(.*?\s)[/tt]
 
(Y00[\S]*)

The \S being "Not whitespace"

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Not sure what that achieves over (YOO) in CountTitles.

It is, however, a slight improvement over my (Y00)(.*?\s) if redone as (Y00)([\S]*) or even Y00([\S]*) if we want to capture the titles rather than count them. Let's wait and see what ClulessChris says.
 
Hi guys thanks for the time you've taken to respond. I've stuck with the second pattern I posted. This is to reduce the risk of matching errant strings such as UK postcode parts, and other random strings of the text I working with.
I'm happy that this is now providing the required results. Still I'm very greatfull for all your input.

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top