【问题标题】:REGEX - how to extract a specific number of rows from a textREGEX - 如何从文本中提取特定数量的行
【发布时间】:2021-08-04 22:43:30
【问题描述】:

我需要了解如何从文本中提取特定数量的行(我想要提取的行数是可变的)。

在这种情况下,我想从07/06/2021 中提取任何内容,直到SOLD FINAL ZI 1

文字

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccc

07/06/2021 P2P  00.00

T d r 0000 R A cc  R A
r : aadr

REF. ------------------

P l p  00.00

P XX/XX/XXXX 0000000000 :00000000000 P R R

A B OO 0000000000 v e: 00.00 n 0000000000

c t 0.00 n

REF. ------------------

P2P  00.00

T d r  0000 R A c R A

rr : Saracie

REF. ------------------

P2P  00.00

T d r 0000 A. B c R A rr : Sanity

REF. ------------------

P l p   00.00

P XX/XX/XXXX 0000000000  00000000000 P R R

D OO 0000000000 V T: 00.00 n 0000000000 c

T 0.00 n

REF. ------------------

XX/XX/XXXX RULAJ ZI 1 3

SOLD FINAL ZI 1

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccc

在正则表达式中,我从\n(\d{2}/\d{2}/\d{4}) 开始以获取数据07/06/2021,但我不知道如何提取其余部分。

提前谢谢你!

【问题讨论】:

  • 试试^(\d{2}\/\d{2}\/\d{4})[\s\S]+SOLD FINAL
  • 谢谢你,这是我用的,它有效:\n(\d{2}\/\d{2}\/\d{4})[\s\S] +SOLD FINAL ZI

标签: regex uipath


【解决方案1】:

您好,欢迎来到 stackoverflow,

您的问题可能无法解决您的实际问题。你真的想“提取特定数量的行”吗?这可能是XYProblem

我喜欢 the solution from MDR 提取直到 SOLD FINAL 的所有内容:
^(\d{2}\/\d{2}\/\d{4})[\s\S]+SOLD FINAL
我喜欢这个,因为我猜你知道最后的单词而不是行数。但我们不能说。

无论如何,要给您问题的答案(因为您的实际问题可能看起来与我们预期的不同),您可以使用这个正则表达式: ^(\d{2}\/\d{2}\/\d{4}).*$(\n^.*$){n}

^                     --> look at the beginning of a row
(\d{2}\/\d{2}\/\d{4}) --> your regex for the date
.*$                   --> also take the rest of the line
(\n^.*$){n}           --> take the next n lines
     \n --> the line break
     ^  --> again: beginning of a new line
     .* --> as much characters as needed to match the next (non greedy)
     $  --> the end of a line
     {n}--> the number of lines you want to extract (replace n ;) )

【讨论】:

  • 谢谢,但实际上我无法使其与您的代码一起使用,但您也提到了 MDR。也许是因为我使用的网站 regexstorm.net,但我必须将该正则表达式添加一个“\n”才能使其工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多