-3

I have an text file with below input, I need to validate the input in ruby whether it is correct format or not?

Need to read an each line of text file and validate that whether the input is matching to Integer,s1-integer,s2-integer,s3-integer,s4-integer format other wise I need to raise an error that file input format is mismatch.

The input line is not limited to 5, it can be any number of lines.

Integer,s1-integer,s2-integer,s3-integer,s4-integer

Example inputs:

1,S1­-88,S2­-53,S3­-69,S4­-64 
2,S1­-92,S2­-86,S3­-93,S4­-77 
3,S1­-53,S2­-59,S3­-72,S4­-59 
4,S1­-60,S2­-52,S3­-85,S4­-62 
5,S1­-85,S2­-53,S3­-74,S4­-61 
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Nataraja B
  • 535
  • 3
  • 15

1 Answers1

3

If I understand you right, you need to validate the following input:

  • a number followed by four elements with a format ,S1­-85

The following pattern matches the input of that type:

\d(\,S\d\-\d\d){4}
  • \d matches a number
  • (\,S\d\-\d\d) matches a group of type ,S1­-85
  • {4} tells to match ,S1­-85 group 4 times
Igor Drozdov
  • 13,491
  • 5
  • 33
  • 50