正则表达式匹配换行符后的所有内容

时间:2019-04-16 14:37:00

标签: regex

我正在尝试编写正则表达式以匹配提交哈希和与该哈希关联的文件。请参阅下面的示例文件:

:6585d0f0ba88767ac3b590f719596d864d73e9c1:

harmonicbalance/src/harmonicbalance/HarmonicBalanceFlowModel.cpp
harmonicbalance/src/harmonicbalance/HbFlutterModel.cpp
:8302994b565553c83a048b8905ae597349d99627:

emp/src/emp/PhasePairSingleParticleReynoldsNumber.h
emp/src/emp/TomiyamaDragCoefficientMethod.cpp
:9da194f17ec08bb20ad1be8df68b78ca137ab18a:

combustion/src/combustion/ReactingSpeciesTransportBasedModel.cpp
combustion/src/complexchemistry/TurbulentFlameClosure.cpp
:6a59f0be1e347a65e525e58742bb304639ea9bc4:

meshing/src/meshing/SurfaceMeshManipulation.cpp
physics/src/discretization/FvIndirectRegionInterfaceManager.cpp
physics/src/discretization/FvIndirectRegionInterfaceManager.h
physics/src/discretization/FvRepresentation.cpp
physics/src/discretization/FvRepresentation.h
:64b7f6d36b11b6cd94c20cad53463b7deef8c85a:

resourceclient/src/resourceclient/ResourcePool.cpp
resourceclient/src/resourceclient/ResourcePool.h
resourceclient/src/resourceclient/RestClient.cpp
resourceclient/src/resourceclient/RestClient.h
resourceclient/src/resourceclient/test/ResourcePoolTest.cpp

这是我想出的正则表达式:

^:([A-Za-z0-9]+):[\n]+([A-Za-z]+\/.*[.a-z])$

此表达式捕获提交哈希和第一个文件。

如何捕获所有文件和所有提交?

1 个答案:

答案 0 :(得分:2)

您可以使用\G使用此符合Java的正则表达式:

(?:^:([A-Za-z0-9]+):|(?!^)\G)\n+([A-Za-z/.-]+)

RegEx Demo

\G在上一场比赛的末尾或首场比赛的字符串开头声明位置。