Author Topic: Validation String  (Read 632 times)

Validation String
« on: November 01, 2018, 10:17:16 am »
I've started working on a new project and we have to identify all equipment installed using a unique identifier, as explained below.  My problem is that I cannot see why the sample beginning 12 is not valid.


Quote
Each active component within a design will be assigned a Unique Identifier (UID). This will be formed in the following manner

{AREACODE}/{TYPE}/{COUNTER}

For those so inclined, here is the validation string
^\d{3}\/[A-Z]{3,8}\/\d{3,5}$

Translation:
 
Translation by Russell Wiles, on Flickr

In the screengrab below, only the matches are highlighted.
 
Samples by Russell Wiles, on Flickr

Apologies for the large picture size - I got bored trying to format them!!

Oaky

  • ACME Fire Safety Officer
  • Audax Club Mid-Essex
    • MEMWNS Map
Re: Validation String
« Reply #1 on: November 01, 2018, 10:25:47 am »
Given that the first element is meant to be exactly 3 digits, I don't see how any of your examples would pass.  Unless I'm being unbelievably dense.
You are in a maze of twisty flat droves, all alike.

85.4 miles from Marsh Gibbon

Audax Club Mid-Essex Fire Safety Officer
http://acme.bike

Re: Validation String
« Reply #2 on: November 01, 2018, 10:35:48 am »
I sort of thought that but I dismissed it as me being dense as well.

It is from an incomplete document but I didn't think it was that incomplete!!

Phil W

Re: Validation String
« Reply #3 on: November 01, 2018, 10:39:55 am »
^^ as above the first part of your regular expression is demanding exactly 3 digits in the the range 0-9 before the /.  Change that to \d{2} or \d{2,3} and you can match on the area code being two or three digits in range 0-9 before the /.

TheLurker

  • Goes well with magnolia.
Re: Validation String
« Reply #4 on: November 01, 2018, 10:43:30 am »
Regexes eh?  Sucks teeth...

I'd expect it to be {1,3} rather than {3} as well, assuming that there's always going to be at least one digit.

Check the beginning of the string for non-printing characters? ACKs, NAKs etc?  Used to get bitten by that all the time working on data inbound over RS-232 back when even the dinosaurs wore short trousers.

ETA.
If you're using the .NET Regex class you could also try setting the multi-line option true.  I've had odd, inexplicable (to me) cases where the line anchors weren't recognised if multi-line was false.
Τα πιο όμορφα ταξίδια γίνονται με τις δικές μας δυνάμεις - Φίλοι του Ποδήλατου

vorsprung

  • Opposites Attract
    • Audaxing
Re: Validation String
« Reply #5 on: November 01, 2018, 11:52:12 am »

$ perl -d -e1

Loading DB routines from perl5db.pl version 1.39_10
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.


  DB<1> $r=qr/^\d{3}\/[A-Z]{3,8}\/\d{3,5}$/;

  DB<2> x "01/FRAME/001" =~ /$r/
  empty array
  DB<3> x "99/WIDGET/123" =~ /$r/
  empty array
  DB<4> x "123/RABBITS/006" =~ /$r/
0  1


the examples don't work with that regular expression.  If the first set of digits is 3 in length then it does
Audaxing Blog follow @vorsprungbike on

Re: Validation String
« Reply #6 on: November 01, 2018, 01:44:53 pm »
Thanks all.  I need to go back to the author about this and other things.

TheLurker

  • Goes well with magnolia.
Re: Validation String
« Reply #7 on: November 02, 2018, 08:52:05 pm »
Bit of tinkering.  The only way I can make this work in .Net is to throw away the anchors (^, $) and, as discussed above, set a range on the repeat interval for the leading digits.

If the ID can contain mixed case alphabetic characters then the simplest way to handle that is to set the ignoreCase flag on match.

Code: [Select]
      string pattern = @"\d{1,3}\/[A-Z]{3,8}\/\d{3,5}";
      string data =
@"01/FRAME/001
02/SYNC/999
99/WIDGET/123
AE/THISISTOOLONG/111
13/match/123
1/noMatch/01
12/SYNC/1112";

      Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
      MatchCollection matches = r.Matches(data);

      foreach (Match m in matches) {
        Console.WriteLine(m.Value);
      }

All but AE/THISISTOOLONG/111 and 1/noMatch/01 are returned.
Τα πιο όμορφα ταξίδια γίνονται με τις δικές μας δυνάμεις - Φίλοι του Ποδήλατου