Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取可选冒号前的所有字母_Php_Regex - Fatal编程技术网

Php 获取可选冒号前的所有字母

Php 获取可选冒号前的所有字母,php,regex,Php,Regex,我有一个PHPExcel的包装器,还有一个名为setColumn的方法,如下所示: public function setColumn($cell = ""){ if(empty($cell)){ $cell = $this->cell; } $this->column = strtoupper(preg_replace("/[^A-Za-z]/", "", $cell)); $this->cell = "{$this->

我有一个PHPExcel的包装器,还有一个名为setColumn的方法,如下所示:

public function setColumn($cell = ""){
    if(empty($cell)){
        $cell = $this->cell;
    }
    $this->column = strtoupper(preg_replace("/[^A-Za-z]/", "", $cell));
    $this->cell   = "{$this->column}{$this->row}";
    return $this;
}
public function setColumn($cell = ""){
    if(empty($cell)){
        $cell = $this->cell;
    }
    $rng          = explode(":", $cell);
    $this->column = strtoupper(preg_replace("/[^A-Za-z]/", "", $rng[0]));
    $this->cell   = "{$this->column}{$this->row}";
    return $this;
}
它工作得很好,但当我使用它时,我可以在A1:D1等范围内通过,当我通过时,我的preg_replace将无法正确替换,它将返回我不想要的AD。我希望它返回A。我不确定实现这一点所需的正则表达式

参数$cell可以包含几个不同的值:

A1应返回A A1:D10应该返回一个 AD10应返回AD AD1:BBB1应返回AD 这个清单可以继续下去,但这是最基本的。那么我能做些什么来实现这一点呢?

试试:

preg_replace("/^([A-Z]+).*$/", "$1", $cell)
它将“保存”第一部分并删除所有其他部分

.

试试:

preg_replace("/^([A-Z]+).*$/", "$1", $cell)
它将“保存”第一部分并删除所有其他部分


.

匹配数字,而不是可选的冒号,然后匹配其后的所有内容

preg_replace("/\d:?.*/", "", $cell); 
或者正如linepogl指出的那样

preg_replace("/\d.*/", "", $cell); 

只要该模式保持不变:字母编号所有其他内容

与数字匹配,而不是可选的冒号,然后是其后的所有内容

preg_replace("/\d:?.*/", "", $cell); 
或者正如linepogl指出的那样

preg_replace("/\d.*/", "", $cell); 

只要这一模式保持不变:字母为其他所有事物编号

在我提交问题后,我几乎立刻就能想出另一个解决方案。我所做的是在:上爆炸,并使用数组中的第一项,如下所示:

public function setColumn($cell = ""){
    if(empty($cell)){
        $cell = $this->cell;
    }
    $this->column = strtoupper(preg_replace("/[^A-Za-z]/", "", $cell));
    $this->cell   = "{$this->column}{$this->row}";
    return $this;
}
public function setColumn($cell = ""){
    if(empty($cell)){
        $cell = $this->cell;
    }
    $rng          = explode(":", $cell);
    $this->column = strtoupper(preg_replace("/[^A-Za-z]/", "", $rng[0]));
    $this->cell   = "{$this->column}{$this->row}";
    return $this;
}

在我提交问题后,我几乎立刻想出了另一个解决方案。我所做的是在:上爆炸,并使用数组中的第一项,如下所示:

public function setColumn($cell = ""){
    if(empty($cell)){
        $cell = $this->cell;
    }
    $this->column = strtoupper(preg_replace("/[^A-Za-z]/", "", $cell));
    $this->cell   = "{$this->column}{$this->row}";
    return $this;
}
public function setColumn($cell = ""){
    if(empty($cell)){
        $cell = $this->cell;
    }
    $rng          = explode(":", $cell);
    $this->column = strtoupper(preg_replace("/[^A-Za-z]/", "", $rng[0]));
    $this->cell   = "{$this->column}{$this->row}";
    return $this;
}

为了解释你在做什么和你想做什么,我在下面解释了正则表达式

您的正则表达式要求将数组[A-Za-z]中未由“^”表示的所有字符替换为大写字母或小写字母

您想要的是从解析数字的第一个实例中删除所有内容。 要匹配数字,请使用\d。 要匹配任何字符,请使用。 要匹配0个或多个之前的令牌,请使用*。这是一个贪婪匹配,在满足下一个标记之前,将匹配尽可能多的字符

如果我们合并。*这将匹配以下所有内容

若要将其放在一起,则/\d.*/应完成此操作

然后


为了解释你在做什么和你想做什么,我在下面解释了正则表达式

您的正则表达式要求将数组[A-Za-z]中未由“^”表示的所有字符替换为大写字母或小写字母

您想要的是从解析数字的第一个实例中删除所有内容。 要匹配数字,请使用\d。 要匹配任何字符,请使用。 要匹配0个或多个之前的令牌,请使用*。这是一个贪婪匹配,在满足下一个标记之前,将匹配尽可能多的字符

如果我们合并。*这将匹配以下所有内容

若要将其放在一起,则/\d.*/应完成此操作

然后


甚至可以减少到/\d.*/甚至可以减少到/\d*/