我有一个字符串值,但由于字符串的填充方式(超出我的控制),\n我在公司名称的正中间有新的行示例。
我想对特定的匹配项执行正则表达式替换,将\n替换为空格。
这是我的输出的一个片段(它可以更改。但所有我试图匹配所有的出现与第一个\n它发现之前的日期。并提取这些之间的文本。\nGBP*\nAA1234567 A random company name - I 03-Mar-2023 BUY 42.6400 42.6900 GBP 1,820.3016 1.0000 1,842.4400\nAA1234568 Another randon company name - H-M 03-Mar-2023 BUY 11.9880 845.6000 GBP 10,137.0528 1.0000 10,159.1700\nAA12345679 Third Party Utilies - Fund - Class\nAA-B Income\n03-Mar-2023 BUY 6.4120 836.9100 GBP 5,366.2669 1.0000 5,388.5200\nAA12345670 Company 4 - M 03-Mar-2023 BUY 205.6830 10.8500 GBP 2,231.6606 1.0000 2,253.7800\nAA2345678 Another random page up company - I 03-Mar-2023 BUY 66.3850 45.4400 GBP 3,016.5344 1.0000 3,038.6500\nASSET SCHEDULE\nPolicy Number 1234-56789\nAA2345679 Company 5 Utilities- M 03-Mar-2023 BUY 76.7370 13.7400 GBP 1,054.3664 1.0000 1,076.4900\nTotal
现在正在返回。
GBP*\nAA1234567 A random company name - I 03-Mar-2023
AA1234568 Another random company name - H-M 03-Mar-2023
AA12345679 Third Party Utilities - Fund - Class\nAA-B Income\n03-Mar-2023
AA12345670 Company 4 - M 03-Mar-2023
AA2345678 Another random page up company - I 03-Mar-2023
ASSET SCHEDULE\nPolicy Number 1234-56789\nAA2345679 Company 5 Utilities- M 03-Mar-2023
但我想检索的是以下内容。
AA1234567 A random company name - I 03-Mar-2023 BUY 42.6400 42.6900 GBP 1,820.3016 1.0000 1,842.4400
AA1234568 Another random company name - H-M 03-Mar-2023 BUY 11.9880 845.6000 GBP 10,137.0528 1.0000 10,159.1700
AA12345679 Third Party Utilities - Fund - Class\nAA-B Income\n03-Mar-2023 BUY 6.4120 836.9100 GBP 5,366.2669 1.0000 5,388.5200
AA12345670 Company 4 - M 03-Mar-2023 BUY 205.6830 10.8500 GBP 2,231.6606 1.0000 2,253.7800
AA2345678 Another random page up company - I 03-Mar-2023 BUY 66.3850 45.4400 GBP 3,016.5344 1.0000 3,038.6500
AA2345679 Company 5 Utilities- M 03-Mar-2023 BUY 76.7370 13.7400 GBP 1,054.3664 1.0000 1,076.4900
这种情况下的第三行包含2个新行Class\nAA-B Income\n
我的模式如下
(?<=\\n).*?([a-zA-Z]{3})-(\d{4})
https://regex101.com/r/aiDk9G/1
如果有更简单的方法请告诉我。
先谢了
尝试了多种模式,但似乎不能完全得到它。
1条答案
按热度按时间yqlxgs2m1#
您可以使用此正则表达式:
RegEx Demo
RegEx演示:
(?<=\\n)
:LookbehindAssert\n
在先前位置的存在(?:
:启动非捕获组[A-Z]+
:匹配1+个大写字母[0-9]
:匹配数字[A-Z0-9]*
:匹配0个或多个大写字母或数字|
或-
:匹配一个-
)
:结束非捕获组(?:\s+\w+)+
:匹配用1+空格分隔的公司.*?
:匹配0+的任意字符(非贪婪)[a-zA-Z]{3}-\d{4}
:匹配month-year
子字符串.+?
:匹配任意字符的1+(非贪婪)(?=\\n)
:前瞻以Assert\n
在下一个位置的存在