仅在字母序列和数字序列之间分割字符串一次

发布于 2024-12-09 21:51:15 字数 295 浏览 8 评论 0原文

我想从可预测格式的字符串中提取两个子字符串。

每个字符串都由字母和数字组成。

输入和输出:

  • MAU120 => MAU120
  • MAUL345 => MAUL345
  • MAUW23 => MAUW23

I want to extract two substrings from a predictably formatted string.

Each string is comprised of letters followed by numbers.

Inputs & Outputs:

  • MAU120 => MAU and 120
  • MAUL345 => MAUL and 345
  • MAUW23 => MAUW and 23

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Hello爱情风 2024-12-16 21:51:15
$matches = array();

if ( preg_match('/^([A-Z]+)([0-9]+)$/i', 'MAUL345', $matches) ) {
    echo $matches[1]; // MAUL
    echo $matches[2]; // 345 
}

如果您需要 MAU,您可以执行以下操作:

/^(MAU[A-Z]*)([0-9]+)$/i

删除末尾的 i 修饰符将使正则表达式区分大小写。

$matches = array();

if ( preg_match('/^([A-Z]+)([0-9]+)$/i', 'MAUL345', $matches) ) {
    echo $matches[1]; // MAUL
    echo $matches[2]; // 345 
}

If you require the MAU you can do:

/^(MAU[A-Z]*)([0-9]+)$/i

Removing i modifier at the end will make the regex case-sensitive.

孤檠 2024-12-16 21:51:15

试试这个正则表达式:

/(\D*)(\d*)/

PHP 代码:

$matches = array();

var_dump( preg_match('/(\D*)(\d*)/', 'MAUL345', $matches) );
var_dump( $matches );

Try this regular expression:

/(\D*)(\d*)/

PHP code:

$matches = array();

var_dump( preg_match('/(\D*)(\d*)/', 'MAUL345', $matches) );
var_dump( $matches );
时光沙漏 2024-12-16 21:51:15

从字面上看你的例子:

<?php
$tests = array('MAU120', 'MAUL345', 'MAUW23', 'bob2', '?@#!123', 'In the MAUX123 middle.');

header('Content-type: text/plain');
foreach($tests as $test)
{
    preg_match('/(MAU[A-Z]?)(\d+)/', $test, $matches);
    $str = isset($matches[1]) ? $matches[1] : '';
    $num = isset($matches[2]) ? $matches[2] : '';
    printf("\$str = %s\n\$num = %d\n\n", $str, $num);
}
?>

产生:

$test = MAU120
$str = MAU
$num = 120

$test = MAUL345
$str = MAUL
$num = 345

$test = MAUW23
$str = MAUW
$num = 23

$test = bob2
$str = 
$num = 0

$test = ?@#!123
$str = 
$num = 0

$test = In the MAUX123 middle.
$str = MAUX
$num = 123

Taken literally from your examples:

<?php
$tests = array('MAU120', 'MAUL345', 'MAUW23', 'bob2', '?@#!123', 'In the MAUX123 middle.');

header('Content-type: text/plain');
foreach($tests as $test)
{
    preg_match('/(MAU[A-Z]?)(\d+)/', $test, $matches);
    $str = isset($matches[1]) ? $matches[1] : '';
    $num = isset($matches[2]) ? $matches[2] : '';
    printf("\$str = %s\n\$num = %d\n\n", $str, $num);
}
?>

Produces:

$test = MAU120
$str = MAU
$num = 120

$test = MAUL345
$str = MAUL
$num = 345

$test = MAUW23
$str = MAUW
$num = 23

$test = bob2
$str = 
$num = 0

$test = ?@#!123
$str = 
$num = 0

$test = In the MAUX123 middle.
$str = MAUX
$num = 123
ゞ花落谁相伴 2024-12-16 21:51:15

当您可以保证先出现一个或多个非数字,然后出现一个或多个数字时,您可以调用 sscanf() 来解析字符串。

preg_match() 相比,本机函数具有多种优势。

  1. 它不返回完整字符串匹配。
  2. 它将允许您根据您使用的格式占位符键入强制转换子字符串。
  3. 它可以返回数组或创建引用变量——具体取决于您提供的参数数量。

代码:(Demo)

$tests = [
    'MAU120',
    'MAUL345',
    'MAUW23',
];

foreach ($tests as $test) {
    sscanf($test, '%[^0-9]%d', $letters, $numbers);
    var_export([$letters, $numbers]);
    echo "\n";
}

输出:(请注意,数字被转换为整数类型)

array (
  0 => 'MAU',
  1 => 120,
)
array (
  0 => 'MAUL',
  1 => 345,
)
array (
  0 => 'MAUW',
  1 => 23,
)

如果您的数字可能以零并且您想保留它们,可以使用 %s 而不是 %d 来捕获非空格子字符串。如果使用 %s,那么数字将被转换为字符串而不是 int 类型。

替代语法:(演示)

foreach ($tests as $test) {
    var_export(sscanf($test, '%[^0-9]%d'));
    echo "\n";
}

When you can guarantee that there will be one or more non-numbers and then one or more numbers, you can call upon sscanf() to parse the string.

The native function has multiple advantages over preg_match().

  1. It doesn't return the fullstring match.
  2. It will allow you to type cast substrings depending on the format placeholder you use.
  3. It can return its array or create reference variables -- depending on the number of parameters you feed it.

Code: (Demo)

$tests = [
    'MAU120',
    'MAUL345',
    'MAUW23',
];

foreach ($tests as $test) {
    sscanf($test, '%[^0-9]%d', $letters, $numbers);
    var_export([$letters, $numbers]);
    echo "\n";
}

Output: (notice that the numbers are cast as integer type)

array (
  0 => 'MAU',
  1 => 120,
)
array (
  0 => 'MAUL',
  1 => 345,
)
array (
  0 => 'MAUW',
  1 => 23,
)

If your numbers might start with zero(s) and you want to retain them, you can use %s instead of %d to capture the non-whitespaces substring. If you use %s, then the digits will be cast as a string instead of int-type.

Alternative syntax: (Demo)

foreach ($tests as $test) {
    var_export(sscanf($test, '%[^0-9]%d'));
    echo "\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文