简体   繁体   English

如何获取与正则表达式匹配的字符串的唯一部分?

[英]How can I grab the only part of a string that matches a regular expression?

例如,如果我的HTML输入为123Smith456%$@#***()NotSmith ,而我只想要字母字符,那么如何使用正则表达式来匹配并抓住Smith并将其放入变量中?

You can do this by using the PREG_OFFSET_CAPTURE option in the preg_match function. 您可以通过使用preg_match函数中的PREG_OFFSET_CAPTURE选项来执行此操作。

Your expression needs to be wrapped in () to group the matches you wish to capture. 您的表达式需要包含在()中,以将要捕获的匹配项分组。 You can have any number of groups, so you can capture various parts and store them in various variables. 您可以有任意数量的组,因此可以捕获各个部分并将它们存储在各个变量中。

For example: 例如:

$string = '123Smith456%$@#***()NotSmith';

preg_match('/(Smith)/', $string, $matches, PREG_OFFSET_CAPTURE);

print_r($matches);

This will output: 这将输出:

Array
(
    [0] => Array
        (
            [0] => Smith
            [1] => 3
        )

    [1] => Array
        (
            [0] => Smith
            [1] => 3
        )

)

If you are looking to extract all the actual "words" you could do something like this: 如果要提取所有实际的“单词”,则可以执行以下操作:

$string = '123Smith456%$@#***()NotSmith';

preg_match('/([A-Za-z]+)/', $string, $matches, PREG_OFFSET_CAPTURE);

print_r($matches);

This will match all occurrences of anything has characters in the range of AZ or az which occur once or more. 这将匹配所有出现一次或多次出现在AZ或az范围内的字符。 Which outputs: 哪个输出:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Smith
                    [1] => 3
                )

            [1] => Array
                (
                    [0] => NotSmith
                    [1] => 20
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => Smith
                    [1] => 3
                )

            [1] => Array
                (
                    [0] => NotSmith
                    [1] => 20
                )

        )

)

See: https://secure.php.net/manual/en/function.preg-match.php 参见: https : //secure.php.net/manual/en/function.preg-match.php

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

相关问题 如何在 PHP 中使用与整个字符串中的给定组匹配的正则表达式,而不是在第一次匹配时停止 - How can i use a regular expression in PHP that matches a given group in the whole string instead of stops at first match 如何在递归正则表达式中反向引用匹配? - How can I backreference matches in a recursive regular expression? 与仅包含字母或字母数字的字符串匹配的正则表达式 - Regular expression that matches a string having alphabets or alphanumeric only 如何使用正则表达式获取字符串的一部分? - How to get part of string with Regular expression? 仅包含非单词字符的字符串匹配的正则表达式 - Regular expression that matches a string only if it contains non-word characters 如何编写仅在匹配三个必需的捕获组时才匹配的正则表达式 - How do I write a regular expression that only matches if match three required capture groups 如何仅删除带有正则表达式的嵌入式图像? - How can I remove only inline images with a regular expression? 我想使用正则表达式从字符串中提取字符串的一部分 - I want to extract a part of the string from a string using regular expression 用于获取字符串部分的正则表达 - Regular expression for getting string part 正则表达式删除字符串的一部分 - regular expression to remove part of string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM