VBA: Help with Regular Expression

Converemon

New Member
Joined
May 23, 2014
Messages
15
I need help generating a regular expression that does not match a certain form of string. The string can be:

-K09+M72+L86+B12 . . .
Or even K09 + M72 +L86 + B12

But cannot be:
-K098+M72+L86+B12
-kshbd+M72+L86+B12
-K98+M72+Ldsjfjhb86+B12
-etc.

Right now I have ^(?!.*^\s*[A-Z][0-9][0-9]\s*(\+\s*[A-Z][0-9][0-9]\s*)*$ as my expression which does seem to work very well with what I'm trying to do. Again I am trying to match strings that do not comply with the standards (i.e. the second group). I hope a fresh pair of eyes can put out what I'm doing wrong.


</SPAN>

<TBODY>
</TBODY>

</SPAN>

<TBODY>
</TBODY>
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I need help generating a regular expression that does not match a certain form of string. The string can be:

-K09+M72+L86+B12 . . .
Or even K09 + M72 +L86 + B12

But cannot be:
-K098+M72+L86+B12
-kshbd+M72+L86+B12
-K98+M72+Ldsjfjhb86+B12
-etc.

Right now I have ^(?!.*^\s*[A-Z][0-9][0-9]\s*(\+\s*[A-Z][0-9][0-9]\s*)*$ as my expression which does seem to work very well with what I'm trying to do. Again I am trying to match strings that do not comply with the standards (i.e. the second group). I hope a fresh pair of eyes can put out what I'm doing wrong.


<tbody>
</tbody>


<tbody>
</tbody>

I do not do Regular Expressions, but there must by a "not" meta-characters (perhaps the bang character?) which could be used... maybe wrap the whole expression with parentheses and put the not meta-character in front of it? Anyway, that is not why I responded, rather, I wanted to mention the VB Like operator which maybe of interest to you. You would have to process your data one at a time in a loop (assuming you have more than one text string to process), but you can test for text that does not match your pattern using it. First though, I assumed those dashes in the front are not part of your text (because I do not see a dash in your pattern string), rather, I assumed you used them like "bullet points". Given that assumption, this test code will test a single text string (embed it in a loop if you have a list of text strings to process)...

Code:
If Not (Relace(TextString, " ", "") Like "[A-Z]##+"[A-Z]##+"[A-Z]##+"[A-Z]##") Then
    '
    '  TextString did not match the pattern, so do whatever to it
    '
End If
 
Upvote 0
I did in fact consider using the Like operator for this, but my main problem is I don't know how many "codes" will be in the string. For example, it could be M71+L09+J82, M71, or even M71+L09+J82+P55+Q21+T57+U82+M08+O99 . . . Apologies for the dashes, they were meant as bullet points.
 
Upvote 0
I did in fact consider using the Like operator for this, but my main problem is I don't know how many "codes" will be in the string. For example, it could be M71+L09+J82, M71, or even M71+L09+J82+P55+Q21+T57+U82+M08+O99 . . . Apologies for the dashes, they were meant as bullet points.
Oh, so that is what the series of dots at the end of your first example meant... all your examples had the same number of codes in them, so I figured that four codes per text string was a fixed requirement. Okay, you can still use the Like operator to test your text strings (rather than using Regular Expressions to do it), consider this...
Code:
Repeats = UBound(Split(TextString, "+")) + 1
If Not (Replace(TextString & "+", " ", "") Like Replace(Space(Repeats), " ", "[A-Z]##+")) Then
    '
    '  TextString did not match the pattern, so do whatever to it
    '
End If
Note that I introduced a new variable named Repeats which should be Dim'med as Long if you, in fact, Dim your variables.
 
Upvote 0
From all of the tests I ran, your method seems to work perfectly for my situation. I am going to implement it into my project right away and I will let you know how it goes. I really appreciate your time. Also, I had another question concerning the "Split" method you used. I am using it to parse the strings in my project ("M71+L22+K55") using "+" as the delimiter. However, when I try to do this, it always skips the very first "code" ("M71"). Do you have any idea what might be happening?
 
Upvote 0
From all of the tests I ran, your method seems to work perfectly for my situation. I am going to implement it into my project right away and I will let you know how it goes. I really appreciate your time. Also, I had another question concerning the "Split" method you used. I am using it to parse the strings in my project ("M71+L22+K55") using "+" as the delimiter. However, when I try to do this, it always skips the very first "code" ("M71"). Do you have any idea what might be happening?

The Split function always produces a zero-based array (even if the Option Base is set to 1), so you must start your loops at 0, not 1
 
Upvote 0
Silly me, haha. I definitely knew that I just don't know why I didn't consider it. Thank you very much for your help; I do really appreciate it.

Also, your method worked perfectly in my project.
 
Upvote 0

Forum statistics

Threads
1,215,329
Messages
6,124,302
Members
449,150
Latest member
NyDarR

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