I want the following function to validate a string if it looks like this:
- Any digit (incl. fraction and decimal)
- optional one of the pieces/bags/boxes synonyms
- optional one of the male/female synonyms
between each may be a space and the order of the 2 optional parts shouldnt matter (“2 box male” == “2 male box”)
but instead of doing anything useful this function excepts everything that starts with a digit :
Function validAmount(Zelle As Variant)
Set regEx = CreateObject("VBScript.RegExp")
regEx.IgnoreCase = True
regEx.Pattern = "\d\s?(pcs|pieces|piece|pc|stk|bags|bag|box|bx|boxes)?\s?(male|m|female|f)?"
If (regEx.test(Zelle)) Then
validAmount = True
Else
validAmount = False
End If End Function
I hope my error isn’t too stupid
edit:
I thought of an additional feature. how could I allow multiple entities of the pattern above seperated by a “,” like “1 box female, 3 bags m, 4pcs male”
ps: both solutions work pretty well but allow something like this “1 male female” or “2 box bag”
edit2:
first of all: I am really thankful for your help! i would have never solved this on my own! and I wanted to click “solved” (cant click “up” cause my reputation is still too low) as soon as everything works as intended. If I should’ve clicked on it earlier I’m sorry. Its my first question here and I’m a little overwhelmed of how fast your answers come 🙂
I think I’m unable to express my wishes 😀 really sorry for that! here is a third try:
only (at least) one of each groups should be allowed. genuine inputs should be:
“# box gender”
“# gender box”
“# box”
“# gender”
“#”
but not:
“# box box” or “# gender gender”
@sln: I think your first looks more like I want it but it allows two instances of the same group even though it looks for me as it shouldnt 🙁 (same for JMax solution)
@JMax: I love your “split” solution! havent even thought of this simple trick 😀 I was so fixed on regex that a havent thought of anything else
Here is a try:
And here is a testing procedure:
I’ve changed boxes and gender into strings so that you can add both in your pattern (I don’t know any way to check the order without that trick. Does anybody have a better idea?
I also added a
$to tell Excel this was the end of the string, else any string begining with a digit would pass.