如何使用Regex查找两个特殊字符之间的特定字符串?

时间:2019-02-19 12:47:09

标签: sql regex powershell

我目前正在两个论坛系统(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放入变量中。由于整个后置字符串还可以包含外部链接(例如http://google.com),因此我不能只捕获数字。

我想从该字符串http://example.com/thread.php?threadid=XYZ中捕获线程ID,该字符串是较大字符串的一部分(论坛论坛)。我猜正则表达式可以用于此。

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

1 个答案:

答案 0 :(得分:0)

在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分配将引发错误。