繁体   English   中英

正则表达式从下面的页面中提取段落

[英]Regular expression to extract paragraph from the page below

我有使用iText从pdf中提取的文本,并将其放入String变量中:

(1) A a, — al'-fah; of Hebrew origin; the first letter of the alphabet;
figurative only (from its use as a numeral) the first: — Alpha.
Often used (usually ajn an, before a vowel) also in composition
(as a contraction from (427) (a]neu,)) in the sense of privation;
so in many words beginning with this letter; occasionally in the
sense of union (as a contraction of (260) (a[ma)).
(2) ÆAarw>n, — ah-ar-ohn'; of Hebrew origin [Hebrew {175}
('Aharown)]; Aaron, the brother of Moses: — Aaron.
(3) ÆAbaddw>n, — ab-ad-dohn'; of Hebrew origin [Hebrew {11}
('abaddown)]; a destroying angel: — Abaddon.
(4) ajbarh>v, — ab-ar-ace'; from (1) (a) (as a negative particle) and (922)
(ba>rov); weightless, i.e. (figurative) not burdensome: — from
being burdensome.
(5) ÆAbba~, — ab-bah'; of Chaldee origin [Hebrew {2} ('ab (Chaldee))];
father (as a vocative): — Abba.
(6) &Abel, — ab'-el; of Hebrew origin [Hebrew {1893} (Hebel)]; Abel,
the son of Adam: — Abel.
(7) ÆAbia>, — ab-ee-ah'; of Hebrew origin [Hebrew {29} ('Abiyah)];
Abijah, the name of two Israelites: — Abia.
(8) ÆAbia>qar, — ab-ee-ath'-ar; of Hebrew origin [Hebrew {54}
('Ebyathar)]; Abiathar, an Israelite: — Abiathar.
(9) ÆAbilhnh>, — ab-ee-lay-nay'; of foreign origin [compare Hebrew {58}
('abel)]; Abilene, a region of Syria: — Abilene.
(10) ÆAbiou>d, — ab-ee-ood'; of Hebrew origin [Hebrew {31}
('Abiyhuwd)]; Abihud, an Israelite: — Abiud.

字符串中的段落以(9)(5) ([0-9])开头,我想使用pagestring.split("regex")提取以该字符序列开头的每个段落。 可以帮忙吗?

这样可以避免在文本中嵌入的“(999)”上产生分裂。 它基于以下假设:行尾在括号内的数字之前,该数字指示段落的开头。 另请注意,示例文本会在第一个带括号的数字之前的任何文本中生成一个空的“段落”,因此为if语句。

  String text = ...;
  String[] paras = text.split( "(?<=(^|\\n))\\(\\d+\\)" );
  for( String para: paras ){
      if( para.length() > 0 ){
          System.out.println( "Para: " + para );
      }
  }

您可以将以下正则表达式"[\\n|.]\\\\([0-9]{1,2}\\\\)"与split方法一起使用,它将从文本中提取所有段落(包括从0到0的数字) 99):

String[] parts=st.split("[\n|.]\\([0-9]{1,2}\\)");

[\\n|.] :仅考虑新段落,忽略段落文本中的(n)

\\\\([0-9]{1,2}\\\\) :匹配()中的任何一组 两位数字。

这是有效的DEMO ,给出了一个包含所有段落的数组。

有关使用正则表达式的更多信息,请参见Java Regex Pattern

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM