正则表达式如何找到模式?

时间:2015-07-09 13:54:17

标签: regex

我需要将下面的文本与Regex语法分开。实际上我找到了dddd-dddddddd-ddd[x]的食谱。什么用文字?我需要得到这个值的字符串,如下所示:"British Journal of Applied Science & Technology"。如何在正则表达式中编写它?

337 British Journal of Applied Science & Technology 2231-0843 5
338 British Journal of Economics, Management & Trade 2278-098X 5
339 British Journal of Education, Society & Behavioural Science 2278-0998 6
340 British Journal of Environment and Climate Change 2231-4784 5
341 British Journal of Mathematics & Computer Science 2231-0851 4
342 British Journal of Medicine and Medical Research 2231-0614 8
343 British Journal of Pharmaceutical Research 2231-2919 4
344 British Microbiology Research Journal 2231-0886 9
345 Bromatologia i Chemia Toksykologiczna 0365-9445 5
346 Budownictwo Górnicze i Tunelowe 1234-5342 5
347 Budownictwo i Architektura 1899-0665 3
348 Budownictwo, Technologie, Architektura 1644-745X 3
349 Builder 1896-0642 2
350 Built Environment 0263-7960 10
351 Bulgarian Journal of Veterinary Medicine 1311-1477 8
352 Bulgarian Medicine 1314-3387 2
353 Bulletin de la Société des sciences et des lettres de Łódź, Série: Recherches sur les déformations 0459-6854 7
354 Bulletin of Alfred Nobel University. Series "Legal Science" 2226-2873 6
355 Bulletin of Geography. Socio-economic Series 1732-4254 10
356 Bulletin of Geography: Physical Geography Series 2080-7686 9
357 Bulletin of the Polish Academy of Sciences. Mathematics 0239-7269 9
358 Business and Economic Horizons 1804-1205 8
359 Business and Economics Research Journal 1309-2448 10
360 Business Process Management Journal 1463-7154 10

7 个答案:

答案 0 :(得分:2)

(?<=\d\s)\D+(?=\s\d)

那应该找到你需要的东西。如果您对它的工作原理感兴趣: 正则表达式的第一部分((?<=\d\s))声明搜索的短语必须在一个数字(\d)后面跟一个空格(\s)。

第二部分(\D+)是实际找到的。它表示任意数量的非数字字符。

第三部分((?=\s\d))确保结果后跟另一个空格和数字。

答案 1 :(得分:1)

你可以使用一个使用lookahead和lookbehind的表达式,如下所示:

(?<=\d{3}\s).*(?=\s\d{4}-)

此表达式需要三个数字,后跟文本前面的空格,四个数字前面有空格,后跟文本后面的短划线。名称本身由直的.*模式匹配。

Demo.

答案 2 :(得分:0)

由于您没有指定目标语言或类似的东西,以下是使用perl的方法:

cat test.txt | perl -pe 's/^\d+\s//' | perl -pe 's/[0-9X "-]+$//'

第二个表达式可能需要根据其余数据的外观进行调整。

打印:

British Journal of Applied Science & Technology
British Journal of Economics, Management & Trade
British Journal of Education, Society & Behavioural Science
British Journal of Environment and Climate Change
[snip]
Bulletin of the Polish Academy of Sciences. Mathematics
Business and Economic Horizons
Business and Economics Research Journal
Business Process Management Journal

答案 3 :(得分:0)

\d+ (.+) ....-.... \d+

提取:

British Journal of Applied Science & Technology
British Journal of Economics, Management & Trade
British Journal of Education, Society & Behavioural Science
British Journal of Environment and Climate Change
British Journal of Mathematics & Computer Science
British Journal of Medicine and Medical Research
British Journal of Pharmaceutical Research
[... cut ...]

答案 4 :(得分:0)

(\d{3})\s([\D]+)(\d{4}-\d{3,4}X?\s\d{1,2})

这会将字符串拆分为3个捕获组:

  
      
  1. 3位数
  2.   
  3. 任何不包含数字的内容,直至下一个数字
  4.   
  5. 最后的引用(假设它以4位开头并且格式一致)
  6.   

请参阅demo here

答案 5 :(得分:0)

我知道你正在寻找REGEX,但如果你想要更直接的东西,看起来你的文档可以使用简单的字符串操作轻松解析。我提出这个想法作为不想使用REGEX的人的替代方案。

String tmp = "340 British Journal of Environment and Climate Change 2231-4784 5";
String ending = tmp.substring(tmp.length() - 11);
tmp = tmp.substring(0, (tmp.length() - 11)); //parse off the ending
StringTokenizer st = new StringTokenizer(tmp, " ");
String index = st.nextToken(); //reads the first int up to the first space.
tmp = tmp.substring(index.length()); //parse front

现在 tmp 是日记的名称,索引是前几个字符,最后的引用保存为结尾 。此方法仅适用于假定所有字符串完全如上所列,或在类似范围内。

答案 6 :(得分:0)

这一个:

(?<=\d\s)\D+(?=\s\d)

效果很好,但我在我的pdf中发现标题可能有数字,例如

338 British Journal of 5Economics, Management & Trade 2278-098X 5

如何正确解析它? PS我用C#(。NET)编写我的应用程序。