Creating a validation for only the first character/text using regex

is there a variation of this that can work like a “left” function to check the first character in name to if it fits into a specified range like “a-e”

To check the first char of the string is in the range ‘a’…‘e’ (lowercase) you can use

regex(., '^[a-e].*')

The above regex will only check if the first character of the entered string is an ‘a’ thru ‘e’. If you may have leading white space characters, or mixed case, or multiple ‘names’ in the string that all must start with a ‘a’ thru ‘e’, then a more complex regex will be required.

2 Likes

Hello @anand057 ,
take care, this will

  1. only control the FIRST character. An entry like “b 123 % -#” or ‘d’ would be allowed
  2. not cover special letters (e.g. in other languages, e.g. ä, ß, é; ç).

I could not manage to get the regex POSIX option [:lower;} or [:alpha:] working in KoBo (Enketo). This might cover also special letters. @Xiphware: Any idea about that?

Best regards

Correct. The above regex will only check if the first character of the entered string is an ‘a’ thru ‘e’. If you may have leading white space characters, or mixed case, or multiple ‘names’ in the string that all must start with a ‘a’ thru ‘e’, then a more complex regex will be required.

Perhaps if you post some examples of what should be accepted, and what you’d be rejected, that would help clarify the precise requirements.

1 Like

I’m not sure what specific regex engine Enketo uses; it may or may not be entirely POSIX compatible. @martijnr can probably best answer.

1 Like

Enketo uses the built-in JavaScript regex engine (and Collect likely the Java engine). We haven’t clearly specified what we support in https://opendatakit.github.io/xforms-spec/#fn:regex, but it seems that may not be practical to do.

I guess, it would be useful if we documented the overlap (safe syntax) between the different data collection tools though.

So sorry about these incompatibilities you found @wroos!

4 Likes