简体   繁体   English

我想使用正则表达式从字符串中提取字符串的一部分

[英]I want to extract a part of the string from a string using regular expression

I have the variable $state="Mississippi Alabama Texas Massachusetts Kansas". 我有变量$ state =“密西西比州阿拉巴马州德克萨斯州马萨诸塞州堪萨斯州”。 I wanna find the word which contains xas in the end and store this word in a array.I am trying for it, but no results: 我想找到最后包含xas的单词并将其存储在数组中。我正在尝试,但没有结果:

<?php
$states="Mississippi Alabama Texas Massachusetss Kansas";
$str="/xa$/";
$string="";
$ee=preg_match( "/xas$/",$str,$matches);
echo $ee;
echo $matches;

You can use word break \\b instead end-of-string ( $ ) and you should also include the other characters of the word you try to match (with \\w* ): 您可以使用分词符\\b代替字符串结尾( $ ),并且还应包括要尝试匹配的单词的其他字符(使用\\w* ):

$states="Mississippi Alabama Texas Massachusetss Kansas";
preg_match("/\w*xas\b/", $states, $matches);
print_r($matches);

output: 输出:

Array( [0] => "Texas" )

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM