Checking if a string contains any numbers

Hi,
I’m new to BigFix so my little issue can be trivial, however I didn’t find my answer by searching for it here or in documentations. I can’t seem to figure out how to check in a smart way if a string contains any numbers.
I can do this by writing a long relevance, something like

Q: exists “banana-4-smoothy” whose (it contains “0” OR it contains “1” OR it contains “3” OR it contains “4” OR it contains “5” OR it contains “6” OR it contains “7” OR it contains “8” OR it contains “9”)
A: True

But I’m sure there is a much faster, much cleaner approach to this. I would like to use the logic to see if a computer is applicable and if a property contains a certain number or not.
Thanks for having this question and hopefully I’m following the topic rules.

This finds items with any number
exists “banana-4-smoothy” whose (it = (regex “.[0-9].”))

edit as needed to find properties containing a certain number

2 Likes

@trn Wow! Thanks! It never crossed my mind to use regular expressions. Now I can use
exists “banana-4-smoothy” whose (it contains (regex “.[0-9].”))
and I get the expected result with a lot cleaner statement.

That leading and trailing period before and after [0-9] means it will only match a case where there is something before and something after the number – for instance this will show false:

Q: exists "banana-4" whose (it contains (regex ".[0-9].")) 
A: False

We can also replace the [0-9] with simply \d and as you can just do a <string> contains <regex> you can simply do this:

"banana-4-smoothy" contains (regex "\d")
2 Likes

the single leading and trailing period are my snafu with inserting formatted code. The “\d” does simplify things though.

2 Likes

@strawgate thanks for adding the valuable information! Is it POSIX Extended Regular Expression Syntax library that I should study to master the regex wisdom for BigFix?

The regex support varies a bit from platform to platform. There’s a pretty lengthy discussion at Digit \d matching in regex

2 Likes