English 中文(简体)
哪一种 per子能够证实代表 bow球的str?
原标题:What perl regex would validate a string representing a bowling score?
  • 时间:2023-07-15 18:50:22
  •  标签:
  • regex
  • perl

我在座右铭:

X-91-55-72-X-X-X-90-82-91X

I want to use perl regex to validate this string is valid. I want to check the following:

  1. 这些框架以干线(-)划定。 必须有10个框架。

  2. 第1至9条必须不是罢工(例如<代码>X)或两个数字,最多不超过10。

  3. 框架10差别很大。 某些有效价值如下:

    • XXX - Three strikes
    • XX7 - Two strikes and a digit of 0 to 9
    • X91 - A strike and two digits adding up to no more than 10
    • 90 - Two digits adding up to less than 10
    • 91X - Two digits adding up to 10 and then a strike
    • 917 - Two digits adding up to 10 follow by a digit of 0-9

While I can write a crap-ton of code to test each scenario, I m sure there s probably one or two regular expressions that would validate the string. My brain simply can not wrap itself around a regex solution. Any help is appreciated.

下面的法典对插图中的有效特性进行了核实:

if (gs =~ /A[X,0-9,-]+z/ iii

我不知道如何确定框架。

问题回答

This is probably not very elegant, but what it s worth, here s a snippet that might meet your needs:

use constant RE_LT10      =>  (?:0[0-9]|1[0-8]|2[0-7]|3[0-6]|4[0-5]|5[0-4]|6[0-3]|7[0-2]|8[0-1]|90) ;

use constant RE_MAX10     =>  (?:0[0-9]|1[0-9]|2[0-8]|3[0-7]|4[0-6]|5[0-5]|6[0-4]|7[0-3]|8[0-2]|9[01]) ;

use constant RE_IS10      =>  (?:19|28|37|46|55|64|73|82|91) ;

use constant RE_LASTFRAME => "(?:XXX|XX[0-9]|X${RE_MAX10}|${RE_LT10}|${RE_IS10}X|${RE_IS10}[0-9])";

sub is_valid
 {
 print  score is  , $_[0], "
";
 return ($_[0] =~ m/^(?:(?:X|${RE_MAX10})-){9}${RE_LASTFRAME}$/ ? 1 : 0);
 }

$scores =  X-91-55-72-X-X-X-90-89-91X ;
print is_valid($scores), "
";

希望会有所帮助。


Edited my answer to take into account potential "0[0-9]" scores, as per markalex remark.

Since this is Perl we can try Code Constructs for some inline addition.
I believe all the conditions are covered.

use strict;
use warnings;

if (  X-91-55-72-X-X-X-90-82-91X  =~ /
     ^
     # Frames 1-9
     (?:
        (?:
           X
         | 
           ( d )                         # (1)
           ( d )                         # (2)
           (??{  (?!)  if ($1 + $2) > 10 })   # Greater than 10 ? Fail
        )
        -
     ){9}
     
     # Frame 10
     (?:
        X
        (?:
           X [Xd] 
         | ( d )                         # (3)
           ( d )                         # (4)
           (??{  (?!)  if ($3 + $4) > 10 })   # Greater than 10 ? Fail
        )
      | 
        ( d )                            # (5)
        (?:
           ( d )                         # (6)
           (??{  (?!)  if ($5 + $6) > 9 })    # Greater than 9 ? Fail
           
         | ( d )                         # (7)
           (??{  (?!)  if ($5 + $7) != 10 })  # Not Equal to 10 ? Fail
           [Xd] 
        )
     )
     $
/x )
{
   print "Valid Bowling page
";
}
else {
   print "Bowling Page is in Error !
";
}




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...