如何在数字的位置将字符串分解为单词

发布于 2024-11-08 21:52:25 字数 195 浏览 0 评论 0原文

我有一些带有字母数字值的字符串数据。例如us01name、phc01name和其他即字母+数字+字母

我想在第一个字符串中获取第一个字母+数字,并在第二个字符串中剩余

我怎样才能在 php 中做到这一点?

I have some string data with alphanumeric value. like us01name, phc01name and other i.e alphabates + number + alphabates.

i would like to get first alphabates + number in first string and remaining on second.

How can i do it in php?

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

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

发布评论

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

评论(3

天生の放荡 2024-11-15 21:52:26

您可以在带有模式捕获标志的数字上使用 preg_split 。它会返回所有碎片,因此您必须将它们重新组合在一起。然而,在我看来,它比完整的模式正则表达式更直观和灵活。另外,preg_split() 未得到充分利用:)

代码:

$str = 'user01jason';
$pieces = preg_split('/(\d+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($pieces);

输出:

Array
(
    [0] => user
    [1] => 01
    [2] => jason
)

You could use preg_split on the digits with the pattern capture flag. It returns all pieces, so you'd have to put them back together. However, in my opinion is more intuitive and flexible than a complete pattern regex. Plus, preg_split() is underused :)

Code:

$str = 'user01jason';
$pieces = preg_split('/(\d+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($pieces);

Output:

Array
(
    [0] => user
    [1] => 01
    [2] => jason
)
傾旎 2024-11-15 21:52:25

您可以使用正则表达式:

// if statement checks there's at least one match
if(preg_match('/([A-z]+[0-9]+)([A-z]+)/', $string, $matches) > 0){
    $firstbit = $matches[1];
    $nextbit  = $matches[2];
}

只需将正则表达式分解为多个部分,以便您知道每个位的作用:

(              Begin group 1
  [A-z]+         As many alphabet characters as there are (case agnostic)
  [0-9]+         As many numbers as there are
)              End group 1
(              Begin group 2
  [A-z]+         As many alphabet characters as there are (case agnostic)
)              End group 2

You can use a regular expression:

// if statement checks there's at least one match
if(preg_match('/([A-z]+[0-9]+)([A-z]+)/', $string, $matches) > 0){
    $firstbit = $matches[1];
    $nextbit  = $matches[2];
}

Just to break the regular expression down into parts so you know what each bit does:

(              Begin group 1
  [A-z]+         As many alphabet characters as there are (case agnostic)
  [0-9]+         As many numbers as there are
)              End group 1
(              Begin group 2
  [A-z]+         As many alphabet characters as there are (case agnostic)
)              End group 2
就像说晚安 2024-11-15 21:52:25

试试这个代码:

preg_match('~([^\d]+\d+)(.*)~', "us01name", $m);
var_dump($m[1]); // 1st string + number
var_dump($m[2]); // 2nd string

输出

string(4) "us01"
string(4) "name"

即使这个更严格的正则表达式也适合你:

preg_match('~([A-Z]+\d+)([A-Z]+)~i', "us01name", $m);

Try this code:

preg_match('~([^\d]+\d+)(.*)~', "us01name", $m);
var_dump($m[1]); // 1st string + number
var_dump($m[2]); // 2nd string

OUTPUT

string(4) "us01"
string(4) "name"

Even this more restrictive regex will also work for you:

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