Task from the book. Write a regular schedule that checks the string variable's compliance with the rule:

  1. there is at least one uppercase letter
  2. there is at least one lowercase letter
  3. these 2 characters go in a row, the order does not matter

For simplicity, we assume that the strings do not contain strings.

I wrote something like this: /[az]*[AZ]+[az]*/ or /[az]*[AZ]+[az]+/

It is proposed to test on lines like "fred" , "Fred" and "frEd" . I found one word with which this template does not match, it is "freD" . How to generalize the regular list to all possible strings?

The answer in the book about the pearl is even worse than my decision.

  • "not consisting entirely of them" means that the word must contain at least one lower case letter? or does it mean that the line should contain any character except the capital letter and at least one capital letter? - NumminorihSF
  • And where to find it is simple that somewhere in the variable there are both small and large ones and whether it is necessary to select what you found. But then you can run ahead to check for small ones then for large ones (?=.*?[az]+).*?[AZ]+ - Mike
  • also interested in how multi-line strings should be processed (sorry for the tautology) - NumminorihSF
  • Lines are considered single-line. Yes, that's right: there must be at least one lower case letter and at least one upper case in the line. Otherwise, the string is rejected. - typemoon
  • @typemoon Means given in the comment fits - Mike

4 answers 4

 /([az][AZ])|([AZ][az])/ 

Doesn't that exactly match the condition?

 foreach (qw/fred FRED fRed Fred freD 1234/) { if (/([az][AZ])|([AZ][az])/) { print "$_ - ok\n"; } else { print "$_ - bad\n"; } } 

conclusion:

 fred - bad FRED - bad fRed - ok Fred - ok freD - ok 1234 - bad 

P.S. Parentheses in the regulars are optional, but I prefer to put them in such a situation.

  • I wonder what the result should be at fREd ? - Grundy
  • @Grundy 1) there is a small letter 2) there is a big 3) they go in a row. All conditions are met - OK. - andy.37
  • Yes, I didn’t check it, I thought he would check in pairs :) - Grundy
  • 1234Fred ? $%^Fr$&^ ? - ReinRaus
  • @ReinRaus The condition does not say that the string should consist only of alphabetic characters. - dionys

If the word begins with the big ones - we are looking for a small one; if with small ones, we’ll look for a big

 \b([AZ]+[az]|[az]+[AZ])[A-Za-z]*\b 

demo

  • but do not share, for what minusuete? - splash58
  • Xs. At the moment the only correct answer) - ReinRaus
  • well, it may still respond ... - splash58

How about [^AZ]+[AZ]+|[AZ]+[^AZ]+ ?

  • Such a regular schedule will count strings and character sets containing numbers. This is hardly acceptable by the condition of the problem. - typemoon
  • [^ AZ] + If I'm not mistaken, the line must start with some character that is not an uppercase letter. But then the string Fred does not match the pattern. - typemoon
  • It seems like the condition of the problem. "searches for lines with capital letters". In the line, as I understand it, there can be any characters. - jekaby
  • Fred, freD, fRed - the same. FRED, fred - no. - kmv
  • here, by and large, it all depends on whether the conditions of the problem can meet characters that are different from [a-zA-Z] , and how they need to be processed. there is a dog about it - a question about that, is it necessary to look for letters that are different from the letters of the Latin alphabet? - NumminorihSF

You can simplify the regular schedule and the solution itself:

 sub check_string { ($_[0]=~/([az]?[AZ][az]?)/ and length $1 > 1) ? 1 : 0; } print check_string($_) for qw/ fREd fRed Fred FRED freD 88a9 /;