Help with Regular Expression pattern

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi, I've managed to identify a string which starts with a Capitol A-Z followed by a number [0-9] and then a period and space.

However the number may be over 10 (so more than one character) and in some case the period and space missing. This may mean
two checks are needed but I can't get either working properly.

Have Googled a bit but can't find a good pattern tutorial for beginners for VBA RE... if you know of one I'd appreciate a link.

Many thanks, ABB
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I'm not a work and have no idea what SkyDrive is - but here's the text I get from that link -


This item might not exist or is no longer available
This item might have been deleted expired or you might not have permission to view it. Contact the owner of this item for more information.
Go to my SkyDrive
 
Upvote 0
The link works fine for me as well, Alexander.
 
Upvote 0
PGC, I've read the link (thank you) which is great for explaining fundamentals. (I've also got Richards file but looking for how to get it going).
There's a lot in this! Suspect it'll take me a while.

With your pattern "^([A-Z]\d+)(\. )?" is there an ignore or make insignificant switch?

So the [A-Z] could be missing but still find a match if it starts with "1. " with 1 being any number?

Or would it be better practice to alter the string you're searching to Mid(thestring,2) ?
 
Upvote 0
Hi PGC, I'm back yet again...

One of things explained in the link isn't working as hoped and I wonder what I'm doing wrong ?

My string is "1. This is a test 7:14"
objRegExp.Pattern = "(\d+[:]\d+)$"

What I hope to achieve is extract the "7:14"

I find objRegExp.Test("1. This is a test 7:14") is true but the instructions

In RegExpr for VB/VBA, you call RegExprResult(1) to get the first stored value, RegExprResult(2) to get the second one, and so on

fail here. Debug.Print RegExprResult(1) gives error 'Sub or Function not defined'.


Thanks, ABB
 
Upvote 0
Hi

See if this helps:

Code:
Sub Test()
Dim regex As Object, regexMatches As Object
Dim s As String

s = "1. This is a test 7:14"
Set regex = CreateObject("VBScript.RegExp")
        
With regex
    .Pattern = "(\d+[:]\d+)$"
    Set regexMatches = .Execute(s)
    Debug.Print regexMatches(0).Value
End With
End Sub

Remark: I'm using late binding because I've problems with setting references in this machine.
 
Upvote 0
That worked spot on PGC, thanks !

I'm just trying (unsuccessfully) to work out how to extract anything with a leading 2 spaces, and an ending two spaces

I tried this pattern

.Pattern = "( )*( )"

But I think it just gives me 4 spaces. LOL I have a way to go here....
 
Upvote 0
Possibly you could use a pattern such as:

.Pattern = "\ \ \S.*?\ \ "

But I'd expect PGC to come up with something slicker.
 
Last edited:
Upvote 0
Oh and how are you finding the add-in? I used it (and the tutorial at regular-expressions.info :)) to come up with that pattern
 
Upvote 0

Forum statistics

Threads
1,216,038
Messages
6,128,447
Members
449,453
Latest member
jayeshw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top