简体   繁体   English

PHP正则表达式防止重复字符串

[英]PHP regex prevent repeated string

Im trying to create a regular expression in PHP which is checking that a string conforms to the following rules: 我试图在PHP中创建一个正则表达式,检查字符串是否符合以下规则:

  • Has 1 - 7 occurrences of one of the following strings (Mon,Tues,Wed,Thurs,Fri,Sat,Sun) 有1到7次出现下列字符串之一(周一,周二,周三,周四,周五,周六,周日)
  • Strings separated by a , 字符串由a分隔,
  • not case sensitive 不区分大小写
  • No strings are repeated. 没有重复的字符串。

I believe I have fulfilled the first three aspects of this with the following: 我相信我已经通过以下方式实现了前三个方面:

/^(Mon|Tues|Wed|Thurs|Fri|Sat|Sun)(,(Mon|Tues|Wed|Thurs|Fri|Sat|Sun)){0,6}$/i

I am struggling to get to grips with preventing any repeats. 我正在努力防止任何重复。 Can anyone advise? 任何人都可以建议吗?

Does it have to be a regex? 它必须是正则表达式吗? If not: 如果不:

$daysStart = 'Mon,Tues,Wed,mon';
$days = strtolower($daysStart);
$days = explode(",", $days); // split on comma
$days = array_unique($days); // remove uniques
$days = implode(",", $days); // join on comma

// Compare new string to original:
if(strtolower($days)===strtolower($daysStart )){ /*match*/ }

This results in a lowercase string of days, seperated by commas. 这会产生一个小写的天数字符串,用逗号分隔。 Not sure what you wanted as output, you might want to save the original input in another far, or ucfirst() the values via an array_map() or something, this is just to show you another method 不确定你想要什么作为输出,你可能想要将原始输入保存在另一个far,或者ucfirst()通过array_map()或其他东西保存,这只是为了向你展示另一种方法


Or my code shorter: 或者我的代码更短:

$daysStart = 'Mon,Tues,Wed,mon';
$days = explode(",", strtolower($daysStart ) );
$days = implode(",", array_unique($days) );
if(strtolower($days)===strtolower($daysStart )){ /*match*/ }

or function (as short code, can be the longer version ofcourse): 或功能(作为短代码,可以是更长版本的课程):

function checkDays($string){
    $days = explode(",", strtolower($string) );
    $days = implode(",", array_unique($days) );
    return (strtolower($days)===strtolower($daysStart)) ? true : false;// *
}

*I could've done just the return and the str-checks, but I prefer to add true/false in a way im sure my returnvalue always is true of false as boolean, not truthy or falsy. *我可以完成返回和str检查,但我更喜欢以某种方式添加true / false我确定我的returnvalue总是为false,因为boolean,而不是truthy或falsy。

^(Mon|Tues|Wed|Thurs|Fri|Sat|Sun)(?:,(?!\1|\2)(Mon|Tues|Wed|Thurs|Fri|Sat|Sun)){0,6}$

You could use this if regex is an absolute requirement but I'd rather recommend Martijn's answer. 如果正则表达式是绝对要求,你可以使用它,但我宁愿推荐Martijn的答案。 It is much more flexible and easier to read. 它更灵活,更易于阅读。

Here is how i tested this in PHP: 以下是我在PHP中测试的方法:

<?php

$subject1 = "Mon,Mon";
$subject2 = "Sun,Mon,Fri,Sun";
$subject3 = "Sat";
$subject4 = "Mon,Wed,Tues,Fri,Wed";
$subject5 = "Mon,Tues";
$pattern = '/^(Mon|Tues|Wed|Thurs|Fri|Sat|Sun)(?:,(?!\1|\2)(Mon|Tues|Wed|Thurs|Fri|Sat|Sun)){0,6}$/i';
print_r(preg_match($pattern, $subject1, $matches) . " " . $subject1 . "\n");
print_r(preg_match($pattern, $subject2, $matches) . " " . $subject2 . "\n");
print_r(preg_match($pattern, $subject3, $matches) . " " . $subject3 . "\n");
print_r(preg_match($pattern, $subject4, $matches) . " " . $subject4 . "\n");
print_r(preg_match($pattern, $subject5, $matches) . " " . $subject5 . "\n");

?>

This outputs: 这输出:

0 Mon,Mon
0 Sun,Mon,Fri,Sun
1 Sat
1 Mon,Wed,Tues,Fri,Wed
1 Mon,Tues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM