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

时间:2015-03-13 07:40:11

标签: java regex itext

我有这个文本,我使用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.

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

2 个答案:

答案 0 :(得分:0)

这可以避免拆分文本中嵌入的“(999)”。它基于这样的假设:行结束位于括号内的数字之前,表示段落的开头。另请注意,示例文本从第一个带括号的数字之前的文本中生成一个空的“段落” - 因此if语句。

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

答案 1 :(得分:0)

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

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

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

     

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

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

有关使用正则表达式的详细信息,请参阅Java Regex Pattern