Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have descr column which I need to validate against multiple string patterns. I tried using metacharacter "\W" with "like" in excel. But i did not get the correct output.

SQL
descr                   descr_NEW
white cat big                yes
small donkey black           no


Private Sub model()
Dim descr_NEW As String, descr As String
Dim strPattern As String: strPattern = "(\W|^)cat(\W|$)"
Dim regEx As New RegExp
row_number = 1

Do
DoEvents
row_number = row_number + 1
descr = Sheets("Tabelle1").Range("A" & row_number)
 
 With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = True
                .Pattern = strPattern
            End With

If descr Like "(\W|^)cat(\W|$)" Then
descr_NEW = "yes"

Else
descr_NEW = "no"


'If regEx.test(descr) Then
'descr_NEW = "cat"
'
'Else
'descr_NEW = "no"


End If

Range("B" & row_number).Value = descr_NEW

Loop Until row_number = 10
End Sub


Instead of "like", I tried regex.test() (piece of code under comments) and it works fine. But i want to acheive the result using "\W " with "like".

Is there any way?

Thanks in advance
priya
Posted
Updated 30-Mar-15 4:07am
v3
Comments
Maciej Los 28-Mar-15 18:39pm    
Sorry, but your question is not clear...
priyae 30-Mar-15 4:01am    
Hello Los,

Thanks for letting me know.

I have a word "whitecatbig" in the column "descr" and I want to extract "cat" out of it.

I tried like this,
If descr Like "(\W|^)cat(\W|$)" Then.....

But I did not get any output.

Is this this the right way to use \W here?

1 solution

You don't need any VBA code for that! Use built-in formula:
VB
=IF(ISNUMBER(FIND("cat",A2))>0,"yes","no")

A2 - is a reference to descr data.

If you would like to use VBA, you need to loop through the collection of cells:
VB
Sub Test
Dim wsh As Worksheet
Dim i As Integer

Set wsh = Thisworkbook.Worksheets("Sheet1")
i = 2
Do While wsh.Range("A" & i)<>""
    wsh.Range("B" & i) = Test4Cat(wsh.Range("A" & i))
    i = i +1
Loop

End Sub

Function Test4Cat(ByVal sInput As String)

    Test4Cat = IIf(InStr(1, LCase(sInput), "cat", vbTextCompare) > 0, "yes", "no")

End Function


Use the same logic with Regex ;)


[EDIT]
As concerns comments to the answer... you have to create custom function to validate if data meets your needs or no.

VB
Function cfValidate(ByVal sDescription As String) As String
Dim vPatterns As Variant
Dim i as Integer
Dim retVal as String

retVal = "yes" 'return default value

vPatterns = Array("pattern1","pattern2","pattern3")

For i=LBound(vPatterns) To UBound(vPatterns)
    'here call Regex
    'if Regex found matches, the string passed into it is invalid
    retVal = "no"
Next i

cfValidate = retVal
End Function


Note: vPatterns is a "collection" (array) of patterns to check if string is valid.

You can use above function as formula:
VB
=cfValidate(A2)


[/EDIT]
 
Share this answer
 
v2
Comments
priyae 30-Mar-15 4:57am    
Thank you very much for the reply.
yeah I agree your answer:)

But I have a doubt.
If I want to validate a column named "descr"against multiple string patterns and I cannot use built-in function.
For example,

if "*3##?*"
else if "[a-z]{2}"
else if"[0-9]*"

How to write conditional regex for this?

Thanks in advance.
Maciej Los 30-Mar-15 5:11am    
Not sure what you mean by "conditional regex"? Please, be more specific and provide more details...
priyae 30-Mar-15 5:59am    
I meant the regular expression with "if else then" construct as Conditional regex.

I have a column descr which has several values

descr
-------
323
a
3street
england

I need to write regex with multiple if else constructs to check for multiple string patterns. I guess here i cannot use built-in "find" function.

I tried like this:

if descr like "*3##?*"
then descr_new ="yes"

else if descr like "[a-z]{2}" then
descr_new ="no"

else if descr like "[0-9]*" then.....

Is there any other way to write regex in excel with multiple if else constructs?
Maciej Los 30-Mar-15 6:52am    
Thank you for clarification. If i understand you correctly, you have to create custom validation function. Regex does not allow to construct multiple if function. I'll show you how to achieve that. Please be patient... wait for response.
[EDIT]
Done! See updated answer.
priyae 30-Mar-15 10:01am    
Thanks for your kind reply.
This is what I was looking for.

So now got an idea of creating custom function :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900