Regular Expression Validation syntax

Hi all,

I am a new user of SMART 6.3 and plan to use it to organise a network of field observers to collect signs of presence of wolves, lynxes and bears in Croatia. I started to adapt Data Model from sample conservation area since it fits to our needs and actually exceeds them in some parts.
I would like to restrict data entry in some existing and newly created fields with a pattern like for Date and Time field (which would be text field since SMART 6 does not support date and time in one field) in a format “dd.mm.yyyy hh:mm”, or in another field as combination of alphanumeric characters, like JK202204151222 (i.e. two letters followed by year, month, day hour and minute).

Every text field can have Regular Expression Validation, but no info about the use of it.
I searched for the instructions in Technical Training Manual SMART 6 and in this forum about the Regular Expression Validation for attribute creation or modification in Data Model. I found no single mentioning of the term, in either of sources. The common wildcard characters like “?”, “#” or “*” do not work or I did not use them properly. Every help is much appreciated!

Thanks!
Josip

Prof. dr. sc. Josip Kusak VMD
Veterinary biology department
Veterinary faculty
University of Zagreb
Heinzelova 55
HR-10000 Zagreb
CROATIA

This is a great idea. To make this usable we’d probably need to have a list of common RegEx codes so that people could choose from a list without having to learn or copy or paste RegEx codes.

Understanding we have users with various levels of skills and that RegEx is easy to mess up, any other suggestions on how we could make enable this very powerful suggestion without creating a risk that people will make mistakes or get stuck?

1 Like

Thanks on your consideration!
A list of common RegEx would be great!
Beside this, how about leaving the field open for not-so-common RegEx with test feed-back capability before save?

Hi all!
It seems the application of a list of common RegEx codes will not come very soon. Is it possible to get any instructions about how to use this field? It would be very useful for us and we need it very soon.
Thanks!
Josip

1 Like

It should be any valid regular expressions will work, we use java coding so any specific implementation would be in the java format, but regular expressions are fairly consistent across all uses, so the typical special characters etc should all work. I tried to dig up a few example off the internet quickly, I haven’t tested all these, but they should work:

^\d{4}-\d{2}-\d{2}$ date in the format nnnn-nn-nn where n is any number , ex: 2022-02-23, it would also allow 9999-99-99 so if you want to enforce valid dates that is more complicated

^((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$ is what I found for matching valid dates, where it is YYYY-MM-DD, see here for more details: https://www.baeldung.com/java-date-regular-expressions

^\d{2}-\d{2}-\d{2} \d{2}:\d{2}$ would allow things like 2022-04-21 09:30 (note you need the 09:30 and not 9:30 to be valid here

you can see the patters for number of of number above, \d{4} requires 4 numbers, you can specify letters as well with something like [a-zA-Z]{2} to allow any regular letters, either case.
Example:
[a-zA-Z]{2}\d{6}

[a-zA-Z]{2} means two letters \d{6} means 6 digits

If you want only uppercase letters, then:
[A-Z]{2}\d{6}

For other needs, I recommend just searching the internet for something like, “regular expression that matches any number of letters” and you usually get good examples to copy.

Here is a general reference that has more scenarios as well:

1 Like

One other important thing to note, I don’t believe these regex patterns are enforced in SMART Mobile yet, I will send a reminder, that should probably on the Mobile development teams task list.

Ah, OK! Good to know, so that at least I do not try in vain.

Hi all,
The good news is that RegEx is implemented in the latest SMART Mobile ver. 416, but i still cannot figure-out how to get time recorded in the format hh:mm, i.e. hours and minutes like 05:35
Writing \d{2}:\d{2} or ^\d{2}:\d{2}$ in the field, does not work for me.

Josip

@justinsteventon said he looked at it and did see some issues. he is updating the regex parsing on smart mobile for the next version I believe(V421).

1 Like

Hi,
Just checked again in ver. 422 of SM and RegEx validation for time still does not work.

Hi Josip,

This works for me. What are you seeing?

Thanks

Hi Justin,
After double checking and the correction of syntax, I found it working for me too!
Thanks!
Josip

Just adding a few more examples of regex so they can all be found in one place in this thread.

For character beyond the basic Latin ones you can specific unicode ranges. You can do that like this:

[\u0100-\u01BF]*

that matches any number of character from codes 0100 through to 01bf, which from here:

is latin extended-A and some of latin extended-B

To match those as well as the basic latin letters you can put all ranges in the square brackets:

[\u0100-\u01BFa-zA-Z]*

That will match any word that uses basic latin letters, and any of those unicode extended-A & B characters, for example I tested this:

abcDEfgĎĹxyZ

1 Like

More useful tips and examples, mostly got the base info from here if you want more details etc:

To match any character at all including extended latin characters you can use these:

\p{Lu} - matches any capital letters
\p{Ll} - matches any lower case letter

So an example is wanting to match a 2-word name with both names captalized:

^[\p{Lu}]{1}[\p{Ll}\p{Lu}][ ][\p{Lu}]{1}[\p{Ll}\p{Lu}]

Explanation:
^- at the beginning
\p{Lu}]{1} - one capital letter
[\p{Ll}\p{Lu}]* - any number of capital or lower case letters. You could remove the \p{Lu} if you want to enforce all the rest to be lower case
[ ] - a space
same thing for word/name #2

example matches:
John Doe
ΣSSabcZfg ÇSabcZfg
JIMMy FalLon

fails to match:
John1 Doe2
John F Kennedy
sam lowercase

Example #2:
Match an id and date stamp like this, ĐŠ202208171157; two capital letters followed by YYYYMMDDHHMM pattern.

^[\p{Lu}]{2}[\d]{12}

Explanation:
^[\p{Lu}]{2} - match two capital letters at the start
[\d]{12} - then 12 digits

example matches:
ĐŠ202208171157
FF123456789012
ZZ999999999999

fails to match:
zz999999999999
ZZ20221311245 (months must have 01, not just 0)

It works!
Thanks Jeff!

I jumped in the previous conclusion to quickly.
The described RegEx syntax works in desktop SMART 7.5.2, but it did not work in the latest two versions of SMART Mobile for Android.
Also, the syntax for two words with first letter capital, was slightly different from the one in example. The syntax which worked for me in SMART Desktop 7.5.2 was:

Two words with first letter capital, Latin letters accepted:
[\p{Lu}]{1}[\p{Ll}][ ][\p{Lu}]{1}[\p{Ll}]

The presence or absence of “^” sign at the beginning did not make difference. Both variants (with and without “^”) worked on SMART Desktop 7.5.2, and did not work on Mobile for Android 422 and 424.