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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退