dorkahemp972157683 2014-07-19 14:44
浏览 70

正则表达式如何从文本中删除所有LONG短划线/连字符和正常破折号?

I am trying to remove long dashes and normal dashes from a text. I am able to remove the single normal dashes but I have problems with the long ones. I am also able to remove them, too, but then it causes problems with the numbers in the text.

For instance the text: asdasd2 34 56 ——————————————-

I do regex like [\u2014\-] and this removes all the long and normal dashes, but also removes all the numbers. [\-] removes the normal dash with no problems. Can anyone help with the correct regex? I want to remove all type of dashes in the text, and replace them with nothing. Thanks in advance

  • 写回答

4条回答 默认 最新

  • dongqin1819 2014-07-19 15:45
    关注

    The snippet below could help you

    <?php
    
    $string = "asdasd2 34 56 ——————————————-";
    $string = preg_replace("/(?:(?=—|\-).)+/", "", $string);
    echo $string; // asdasd2 34 56
    
    ?>
    

    The two expressions below should also replace all kind of hypens

    [\p{Pd}]+
    [\x{2010}-\x{2015}|\x{002D}|\x{2212}|\x{FE58}|\x{FE63}|\x{FF0D}]+
    

    but for some reason I'm getting an error or a string with this weird character (�). That's how I came up with the first solution.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测