doulu8537 2015-05-16 22:54
浏览 248
已采纳

正则表达式删除除数字和一个字符之外的所有内容

I need to remove everything but numbers and, if exists one character from a string. It's a street name I need to extract the house number of. It is possible that there is some more content after the string, but not neccessarely.

The original string is something like

Wagnerstrasse 3a platz53,eingang 3,Zi.3005 

I extract the street with number like this:

preg_match('/^([^\d]*[^\d\s]) *(\d.*)$/', $address, $match);

Then, I do an if statement on "Wagnerstrasse 3a"

if (preg_replace("/[^0-9]/","",$match[2]) == $match[2])

I need to change the regex in order to get one following letter too, even if there is a space in between, but only if it is a single letter so that my if is true for this condition / Better a regex that just removes everything but below:

Wagnerstrasse 3a       <-- expected result: 3a
Wagnerstrasse 3 a      <--- expected result 3 a 
Wagnerstrasse 3        <--- expected result 3
Wagnerstrasse 3 a bac  <--- expected result 3 a
  • 写回答

3条回答 默认 最新

  • doushi7314 2015-05-19 21:06
    关注

    After doing some more research and hours of checking addresses (so many addresses) on the topic I found a solution which, until now, didn't fail. Might be that I didn't realize it, but it seems to be quite good. And it's a regex one has not seen before... The regex fails if there are no numbers in the line. So I did some hacking (mention the millions of nines...)

    Basically the regex is excellent for finding numbers at the end and preserves numbers in the middle of the text but fails for above mentionend fact and if the street starts with a number. So I did just another little hack and explode the first number to the back and catch it as number.

    if ($this->startsWithNumber($data))
    {
        $tmp = explode(' ', $data);
        $data = trim(str_replace($tmp[0], '', $data)) . ' ' . $tmp[0];
    }
    if (!preg_match('/[0-9]/',$data)) 
    {
        $data .= ' 99999999999999999999999999999999999999999999999999999999999999999999999';
    }
    $data = preg_replace("/[^ \w]+/",'',$data);
    
                        $pcre = '/\A\s*
    (.*?) # street
    \s*
    \x2f? # slash
    (
        \pN+\s*[a-zA-Z]? # number + letter
        (?:\s*[-\x2f\pP]\s*\pN+\s*[a-zA-Z]?)* # cut
    ) # number
    \s*\z/ux';
                        preg_match($regex, $data, $h);
    
    $compare = strpos($h[2],'999999999999999999999999999999999999999999999999999999999999999999999999');
                        if ($compare !== false) {
                            $h[2] = null;
                        }
                        $this->receiverStreet[] = (isset($h[1])) ? $h[1] : null;
                        $this->receiverHouseNo[] = (isset($h[2])) ? $h[2]  : null;
    

    public function startsWithNumber($str)
        {
            return preg_match('/^\d/', $str) === 1;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?