添加撇号到正则表达式匹配

时间:2015-05-07 21:42:32

标签: php regex

以下是我的文字来源:

public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool flag;
            return value != null && bool.TryParse(value.ToString(), out flag) && flag
                ? Visibility.Visible
                : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value is Visibility && (Visibility)value == Visibility.Visible;
        }
    }

这是我的代码:

           $data['DATA_TYPE'] = 'enum('Film Studio','Website','Advertiser','Distributer')';

结果如下:

            $data = static::getColumnInfo($resourceClassName, $columnName);
            $data = $data['DATA_TYPE'];
            preg_match('/enum\((.*)\)$/',$data,$matches);
            $vals = explode(',',$matches[1]);

如您所见,结果在每个索引中包含开头和结尾的引号

我需要修改正则表达式按其当前操作加上开始和结束引号进行分组,这样我就可以保持引号但与预期的外部引号匹配。

1 个答案:

答案 0 :(得分:1)

您可以使用:

$s = "Bullet \xe2\x80\xa2 character";
echo htmlentities ($s, ENT_NOQUOTES, 'utf-8'), "\n";

即。从$vals = explode(',', preg_replace("/^'|'$|'(?=,)|(?<=,)'/", "", $matches[1])); 字符串中删除单引号,然后使用逗号进行爆炸。

它会得到:

$matches[1]

<强>解释

print_r ( $vals );
Array
(
    [0] => Film Studio
    [1] => Website
    [2] => Advertiser
    [3] => Distributer
)