首页 > 解决方案 > 如何使用正则表达式在两个特殊字符之间查找特定字符串?

问题描述

我目前正在两个论坛系统(Woltlab Burning Board 2 -> MyBB 1.8)之间迁移 sql 数据。

我需要将内部链接从http://example.com/thread.php?threadid=XYZ更改 为http://example.com/showthread.php?tid=ABC。线程 ID 将在两个系统之间更改,因此我无法进行简单的字符串替换。

我已经捕获了所有包含http://example.com/thread.php?threadid=的帖子。现在我需要将唯一 ID 放入变量中。由于整个 post-string 还可以包含外部链接(例如http://google.com),所以我之前不能只捕获数字。

我想从这个字符串http://example.com/thread.php?threadid=XYZ中捕获 Thread-ID ,它是一个更大的字符串(论坛帖子)的一部分。我想 Regex 可以用于此。

任何帮助将不胜感激!谢谢!

标签: sqlregexpowershell

解决方案


在 PowerShell 中,以下将捕获线程 ID:

$URI = "http://example.com/thread.php?threadid=2438&Query=GetSomething?2345"
$tid = ($URI | select-string -pattern "threadid=(\d+)").matches.groups.value[1]
$tid

如果没有要捕获的线程 id,$tid分配将抛出错误。


推荐阅读