【问题标题】:How can I trim a string starting with a specific expression? [closed]如何修剪以特定表达式开头的字符串? [关闭]
【发布时间】:2012-04-19 13:44:20
【问题描述】:

如何使用 Notepad++ 从文件中的所有 url 中查找和删除 utm_content 参数?

样本数据:

http://example.com/content1.html?utm_content=product1
http://example.com/abc2.html?utm_content=homepage
http://example.com/test/?utm_content=sku1234
http://example.com/runapp?utm_content=31231KS
http://example.com/blabla?utm_content=nl-laptops-tablets

想要的结果:

http://example.com/content1.html
http://example.com/abc2.html
http://example.com/test/
http://example.com/runapp
http://example.com/blabla

注意:据我了解,Notepad++ 正则表达式使用标准 PCRE (Perl) 语法。

【问题讨论】:

  • 这不是一个编程问题,因为您只是在处理文本编辑器;因此,我相信它属于超级用户。
  • @Chris Morgan:Notepad++ 主要由程序员使用,所以 Stack Overflow 上的问题就好了。
  • @BoltClock'saUnicorn:如果它与编码(操作代码)直接相关,我同意它仍然属于这里,但是当它只是处理所有可见意图时只是一个列表的 URL,我觉得它不属于这里。但这还不足以引起大惊小怪。

标签: regex notepad++ pcre


【解决方案1】:

据我了解您的需求:

搜索:

\?utm_content=.*?"

并替换为"

【讨论】:

  • 选择所有内容,直到换行。样本数据应该是这样的:href="http://example.com/content1.html?utm_content=nl-laptops-tablets",我需要输出这个href="http://example.com/content1.html"。很抱歉没有从一开始就解释得更好。
  • @Bogdan:我不太明白你的意思。上面的表达式适用于您提供的示例。
  • 这里是样本数据的更广泛视图:<map name="meniu" id="meniu"> <area shape="rect" coords="94,1,174,28" href="http://www.example.com/laptops-tablets.html?utm_content=nl-laptops-tablets" alt="Portables" /> <area shape="rect" coords="181,1,318,28" href="http://www.example.com/computers-and-servers.html?utm_content=nl-computers-and-servers" alt="computers and servers" /> <area shape="rect" coords="397,0,494,25" href="http://www.example.com/photo-video.html?utm_content=nl-photo-video" alt="photo-video" />
  • 当我使用\?utm_content=.* 时,它会选择从?utm_content 开始直到行尾的所有内容
  • @Bogdan:是的,当然:preg_replace('/\?utm_content=.*?"/', '"', $string);
【解决方案2】:

搜索\?utm_content=\w+\?utm_content=.*,如果这是唯一的一个参数,则将其替换为零字符串。

【讨论】:

  • 这部分有效:\?utm_content=\w+。但是如果我有一个包含- 字符的参数,例如这个:?utm_content=nl-laptops-tablets,它会选择序列直到第一个-
  • @ChrisMorgan 因为 OP 不够具体,所以整个 URL 可能看起来如何。
  • @Bogdan 正确。我的回答非常严格地处理这些例子! ;-)
【解决方案3】:

使用标准搜索和替换 (Ctrl + R)

编辑:

搜索: utm_content

替换:

按查找,然后按替换休息。

【讨论】:

  • 这不是 OP 所要求的。
  • OP 的原始问题含糊不清。对于原始问题,一个简单的搜索和替换就足够了。编辑后的问题更清晰,确实需要 RegEx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多