1

I am looking for pattern matching in GridDB using NewSQL to return values true when matched and false when not matched.

The string can start with

  1. 91 followed 33 followed by 8 digit number followed by upper case letters eg 913312345678JAYP (should return true)

  2. 0 followed 33 followed by 8 digit number followed by upper case letters eg 03312345458MARYP (should return true)

  3. Starts with 33 followed by 8 digit number followed by upper case letters eg 3312345458TOMH (should return true)

If the string does not start with 91/0/none followed by 33 and 8 digit number and then letters it should return False. Below are some examples:

53312345458RYANH --invalid

3312345458 --invalid

03312345458JAYP8 --invalid

Can anyone help with with solution. I used the function GLOB and it does not have the OR option so that I can choose between 0/91/none.

5
  • Can't you just use three glob expressions? glob('33[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]*', your_column) OR glob('033[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]*', your_column) OR glob('9133[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]*', your_column) Commented Sep 6 at 7:24
  • Try using ^(0|91)?33\d{8}[A-Z]+$. Regex 101 - btw why 03312345458JAYP is invalid? Commented Sep 6 at 7:36
  • 2
    @MichałTurczyn glob != regex Commented Sep 6 at 12:13
  • 1
    @PaulT. That was a typeo I corrected the last example. Commented Sep 6 at 16:55
  • @MatBailie tried this glob('033[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]*', '03312345458JAYP8') and returns true whereas should be false. The string should end with letters and no numbers in it. Commented Sep 6 at 17:18

1 Answer 1

0

To satisfy the uppercase string ending condition, you must use a pattern ending in "[A-Z]*[A-Z]". The working solution is as follows:

SELECT
  val,
  ( ( GLOB('33[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]', RTRIM(val, cs.cs)) OR
      GLOB('033[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]', RTRIM(val, cs.cs)) OR
      GLOB('9133[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]', RTRIM(val, cs.cs)) ) AND
    SUBSTR(val, -1, 1) BETWEEN 'A' AND 'Z' )
FROM (SELECT '913312345678JAYP' AS val UNION ALL
      SELECT '03312345458MARYP' UNION ALL
      SELECT '3312345458TOMH' UNION ALL
      SELECT '53312345458RYANH' UNION ALL
      SELECT '3312345458' UNION ALL
      SELECT '03312345458JAYP8') AS t
CROSS JOIN (SELECT 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' AS cs) AS cs;

The result.

+------------------+-------+
| val              |       |
+------------------+-------+
| 03312345458MARYP | true  |
| 53312345458RYANH | false |
| 913312345678JAYP | true  |
| 3312345458       | false |
| 03312345458JAYP8 | false |
| 3312345458TOMH   | true  |
+------------------+-------+
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Andrei the solution works for all the cases but for 3312345458 it should return False as after the digits it should just have letter.
I have corrected my answer. The fragment SUBSTR(val, -1, 1) BETWEEN 'A' AND 'Z' can be replaced with GLOB('*[A-Z]', val).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.