字母之间只允许一个空格
I don't know almost anything about regex, but I need to allow only one space between letters, this is what I have done with another questiona and answer, plus random tries with regex101:
/^(\d){1,}(\:[A-Za-z0-9-]+([ a-zA-Z0-9-]+)?)?\:(\d){1,}(?:\.(\d){1,2})?$/m
The fomat should be:
[integer]:[optional label :][integer/decimal]
Example:
12:aaa:12.31
56:a s f:15
34:45.8
I have done some random tries without any success,I'm only able to allow infinite space, could someone help me? I have also looked at others answers, but I couldn't implement in my regex.
Check if error:
preg_match_all('/^(\d+)(:[A-Z0-9-]+(?: [A-Z0-9-]+)*)?:(\d+(?:\.\d{1,2})?)$/mi', $_POST['ratetable'], $out);
if($out[0]!=explode("
",$_POST['ratetable'])){
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array(0=>'Invalid price table at line: '.implode(", ", array_diff_key(array_flip(explode("
",$_POST['ratetable'])),array_flip($out[0])))));
exit();
}
我几乎不知道有关正则表达式的任何内容,但我需要在字母之间只允许一个空格,这就是 我已经完成了另一个问题和答案,以及使用regex101的随机尝试: p>
/ ^(\ d){1,}(\:[A-Za-z0-9 - ] +([a-zA-Z0-9 - ] +)?)?\:(\ d){1,}(?:\。(\ d){1,2})?$ / m
code> pre>
fomat应该是: p>
[integer]:[可选标签:] [整数/小数]
代码> PRE>
示例: p>
12:AAA:12.31 \ N56:ASF:15 \ N34:45.8
code> pre>
我做了一些随机尝试而没有任何成功,我只能允许无限空间,有人可以帮助我吗? 我也查看了其他答案,但我无法在我的正则表达式中实现。 p>
检查是否有错误: p>
preg_match_all(' / ^(\ d +)(:[A-Z0-9 - ] +(?:[A-Z0-9 - ] +)*)?:( \ d +(?:\。\ d {1,2}) ?)$ / mi',$ _POST ['ratetable'],$ out);
if($ out [0]!= explode(“
”,$ _ POST ['ratetable'])){
header ('Content-Type:application / json; charset = utf-8');
echo json_encode(array(0 =>'行无效价格表:'。implode(“,”,array_diff_key(array_flip(explode( “
”,$ _ POST ['ratetable'])),array_flip($ out [0])))));
exit();
}
code> pre>
You could make the regex a bit shorter and allow a single space in between each letter like this:
/^(\d+)(:[A-Z0-9-]+(?: [A-Z0-9-]+)*)?:(\d+(?:\.\d{1,2})?)$/gmi
If you want to capture only the label (and exclude the :
) in the capture, you can use this one.
You're nearly there:
(?: [a-zA-Z0-9-]+)*
the above would mean: space followed by at least one in the character group; the latter group any number of times, without capturing
I think
^\d+:([^:]+:)?\d+(\.\d+)?$
might do it...
It means: <int> + ":" + [label + ":"] + <float>
where label
can be anything but :
.
You can use preg_match
$str = 'Another Example String 1_2-3';
echo $str . "<br />
";
if (preg_match('#^(?:[\w-]+ ?)+$#', $str)){
echo 'Legal format!';
} else {
echo 'Illegal format!';
}