在非字母数字字符以及数字和非数字之间的位置拆分字符串

发布于 2024-12-03 22:50:01 字数 363 浏览 0 评论 0原文

我试图通过非字母数字分隔字符以及数字和非数字的交替来分割字符串。最终结果应该是由字母字符串和数字字符串组成的平面数组。

我正在使用 PHP,并且想使用 REGEX。

示例:

  • ES-3810/24MX 应变为 ['ES', '3810', '24', 'MX']
  • CISCO1538M 应变为['CISCO' , '1538', 'M']

输入文件序列可以是数字或字母。

分隔符可以是非 ALPHA 和非 DIGIT 字符,也可以是 DIGIT 序列到 APLHA 序列之间的更改,反之亦然。

I'm trying to split a string by non-alphanumeric delimiting characters AND between alternations of digits and non-digits. The end result should be a flat array of consisting of alphabetic strings and numeric strings.

I'm working in PHP, and would like to use REGEX.

Examples:

  • ES-3810/24MX should become ['ES', '3810', '24', 'MX']
  • CISCO1538M should become ['CISCO' , '1538', 'M']

The input file sequence can be indifferently DIGITS or ALPHA.

The separators can be non-ALPHA and non-DIGIT chars, as well as a change between a DIGIT sequence to an APLHA sequence, and vice versa.

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

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

发布评论

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

评论(3

热鲨 2024-12-10 22:50:01

匹配所有出现的正则表达式的命令是 preg_match_all(),它输出结果的多维数组。正则表达式非常简单...任何数字 ([0-9]) 一次或多次 (+) 或 (|) 任何字母 ([Az]) 一次或多次 (+)。请注意大写 A 和小写 z 以包含所有大写和小写字母。

为了方便起见,包含了 textarea 和 php 标签,因此您可以放入 php 文件并查看结果。

<textarea style="width:400px; height:400px;">
<?php

foreach( array(
        "ES-3810/24MX",
        "CISCO1538M",
        "123ABC-ThatsHowEasy"
    ) as $string ){

    // get all matches into an array
    preg_match_all("/[0-9]+|[[:upper:][:lower:]]+/",$string,$matches);

    // it is the 0th match that you are interested in...
    print_r( $matches[0] );

}

?>
</textarea>

在文本区域中输出:

Array
(
    [0] => ES
    [1] => 3810
    [2] => 24
    [3] => MX
)
Array
(
    [0] => CISCO
    [1] => 1538
    [2] => M
)
Array
(
    [0] => 123
    [1] => ABC
    [2] => ThatsHowEasy
)

The command to match all occurrances of a regex is preg_match_all() which outputs a multidimensional array of results. The regex is very simple... any digit ([0-9]) one or more times (+) or (|) any letter ([A-z]) one or more times (+). Note the capital A and lowercase z to include all upper and lowercase letters.

The textarea and php tags are inluded for convenience, so you can drop into your php file and see the results.

<textarea style="width:400px; height:400px;">
<?php

foreach( array(
        "ES-3810/24MX",
        "CISCO1538M",
        "123ABC-ThatsHowEasy"
    ) as $string ){

    // get all matches into an array
    preg_match_all("/[0-9]+|[[:upper:][:lower:]]+/",$string,$matches);

    // it is the 0th match that you are interested in...
    print_r( $matches[0] );

}

?>
</textarea>

Which outputs in the textarea:

Array
(
    [0] => ES
    [1] => 3810
    [2] => 24
    [3] => MX
)
Array
(
    [0] => CISCO
    [1] => 1538
    [2] => M
)
Array
(
    [0] => 123
    [1] => ABC
    [2] => ThatsHowEasy
)
七禾 2024-12-10 22:50:01
$str = "ES-3810/24MX35 123 TEST 34/TEST";
$str = preg_replace(array("#[^A-Z0-9]+#i","#\s+#","#([A-Z])([0-9])#i","#([0-9])([A-Z])#i"),array(" "," ","$1 $2","$1 $2"),$str);
echo $str;
$data = explode(" ",$str);
print_r($data);

我想不出更“干净”的方式了。

$str = "ES-3810/24MX35 123 TEST 34/TEST";
$str = preg_replace(array("#[^A-Z0-9]+#i","#\s+#","#([A-Z])([0-9])#i","#([0-9])([A-Z])#i"),array(" "," ","$1 $2","$1 $2"),$str);
echo $str;
$data = explode(" ",$str);
print_r($data);

I could not think on a more 'cleaner' way.

看透却不说透 2024-12-10 22:50:01

生成所需平面输出数组的最直接的 preg_ 函数是 preg_split()

因为非字母数字字符序列两侧的字母数字字符的组合并不重要,所以您可以贪婪地拆分非字母数字子字符串,而无需“环顾四周”。

处理完初步障碍后,然后在数字和非数字之间或非数字和数字之间的零长度位置上进行分割。

/             #starting delimiter
[^a-z\d]+     #match one or more non-alphanumeric characters
|             #OR
\d\K(?=\D)    #match a number, then forget it, then lookahead for a non-number
|             #OR
\D\K(?=\d)    #match a non-number, then forget it, then lookahead for a number
/             #ending delimiter
i             #case-insensitive flag

代码:(演示)

var_export(
    preg_split('/[^a-z\d]+|\d\K(?=\D)|\D\K(?=\d)/i', $string, 0, PREG_SPLIT_NO_EMPTY)
);

preg_match_all() 不是一个愚蠢的技术,但它不返回数组,它返回匹配数并生成一个引用变量,其中包含需要访问第一个元素的二维数组。诚然,该模式更短且更容易遵循。 (演示

var_export(
    preg_match_all('/[a-z]+|\d+/i', $string, $m) ? $m[0] : []
);

The most direct preg_ function to produce the desired flat output array is preg_split().

Because it doesn't matter what combination of alphanumeric characters are on either side of a sequence of non-alphanumeric characters, you can greedily split on non-alphanumeric substrings without "looking around".

After that preliminary obstacle is dealt with, then split on the zero-length positions between a digit and a non-digit OR between a non-digit and a digit.

/             #starting delimiter
[^a-z\d]+     #match one or more non-alphanumeric characters
|             #OR
\d\K(?=\D)    #match a number, then forget it, then lookahead for a non-number
|             #OR
\D\K(?=\d)    #match a non-number, then forget it, then lookahead for a number
/             #ending delimiter
i             #case-insensitive flag

Code: (Demo)

var_export(
    preg_split('/[^a-z\d]+|\d\K(?=\D)|\D\K(?=\d)/i', $string, 0, PREG_SPLIT_NO_EMPTY)
);

preg_match_all() isn't a silly technique, but it doesn't return the array, it returns the number of matches and generates a reference variable containing a two dimensional array of which the first element needs to be accessed. Admittedly, the pattern is shorter and easier to follow. (Demo)

var_export(
    preg_match_all('/[a-z]+|\d+/i', $string, $m) ? $m[0] : []
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文