regex - Powershell正则表达式:如果整个字符串包含的句号既不是开头也不是结尾,则匹配整个字符串

标签 regex powershell

我已经坚持了大约2天。不幸的是,我只能使用powershell(我不擅长)。我想使用正则表达式匹配以下条件:
hxxp:// www [。]网站[。] org
google.com
www.google [。] com
foob​​://geller.xyz
hxxps://网站[。] net / tree / branch / etc
我正在查看被散布和伪装的网址和域(用于IOC)。 url / domain具有所有不同的格式,但它们始终包含anycharacter.anycharacter。我认为最好的匹配方式是,如果字符串的两端都有一个句点,然后再与字符串的开头和结尾进行匹配。我最接近的是:

^.*\b[^.]+$\b
但是,我所做的任何尝试都没有得到积极的结果。如果有人有任何想法,我将不胜感激。为了表明我并不懒惰,以下是我为其他IOC所拥有的(我只是被困在其中):
#Select a file with a dialog. TXT only

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    InitialDirectory = [Environment]::GetFolderPath('Desktop')
    Filter = 'TXT (*.txt)|*.txt'
}
[void]$FileBrowser.ShowDialog()
$FileBrowser.FileNames

#Sets file & applies set string while creating first ouput file

#First regex matches IPV4 <-- works well!
$input_path = $FileBrowser.FileNames
$output_file = ‘C:\Users\output.csv'
$regex = ‘\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b’
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file


#Second regex2 matches  domains  <- is a problem
$regex2 = '\b^.*[^.]+$\b'
select-string $input_path -Pattern $regex2 -AllMatches | % { $_.Matches } | % { $_.Value } | Out-File -FilePath C:\Users\01100\Desktop\Folder\output.csv -Append

#Third matches any file extension <--- works well!
$regex3 = '^\.[a-zA-Z0-9]+$'
select-string $input_path -Pattern $regex3 -AllMatches | % { $_.Matches } | % { $_.Value } | Out-File -FilePath C:\Users\01100\Desktop\Folder\output.csv -Append

#Fourth matches any hash  <--- works well!
$regex4 = '[A-Fa-f0-9]{15,}'
select-string $input_path -Pattern $regex4 -AllMatches | % { $_.Matches } | % { $_.Value } | Out-File -FilePath C:\Users\01100\Desktop\Folder\output.csv -Append

#Fifth matches defanged IPs  <---works well!
$regex5 = '\b\d{1,3}[^b]\.[^b]\d{1,3}[^b]\.[^b]\d{1,3}[^b]\.[^b]\d{1,3}\b'
select-string $input_path -Pattern $regex5 -AllMatches | % { $_.Matches } | % { $_.Value } | Out-File -FilePath C:\Users\01100\Desktop\Folder\output.csv -Append

最佳答案

如果我理解正确,那么您想匹配代表域名或url的所有行吗?您会发现,这不是小事。存在各种用于验证域名或url的正则表达式示例(例如herehere)。但是,要求它们越精确,它们将变得越复杂。
对于您而言,这将更加困难,因为您使用不同的格式(有时带有或不带有方案,或查询字符串)。
正则表达式需要达到的精度取决于您的用例以及您愿意投入多少工作。根据您的示例和您的问题标题,我想您需要一个非常基本的版本。
我建议使用此方法,它应适用于最常见的情况:

'^([a-z0–9-]+://)?([a-z0–9-]+\.)+[a-z0–9-]+(/.*)?$'
简短说明:([a-z0–9-]+://)?在开始时检查可选模式(没有特定的模式)([a-z0–9-]+\.)+[a-z0–9]+域含。可选子域,然后是顶级域(/.*)?匹配可选查询字符串(未经验证)
如果需要更高的准确性,则可以将此正则表达式用作过滤输入的第一步,然后对输入字符串进行进一步的测试。您可以validate if it's a valid urlcheck if the domain name exists

关于regex - Powershell正则表达式:如果整个字符串包含的句号既不是开头也不是结尾,则匹配整个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64624764/

相关文章:

powershell - 在 PowerShell 中异步调用 System.Action 实例

非交互模式下的 Powershell

powershell - 使用 PowerShell 从每组中选择前 5 个项目

php - 为什么我们需要逃脱! < > : = - in php regular expressions?

python - 仅提取两个正则表达式模式之间的字符串部分

php - 拉脱维亚公民个人代码的正则表达式

powershell - PowerShell 中的 "exit"到底是什么?

java - 安全:filter-chain pattern match url have symbol '?'

javascript - 在 IE 和 Chrome 中工作的代码在 Firefox 中不起作用

powershell - 为什么 Powershell Import-Csv 无法在此 CSV 文件上正常工作?