使用正则表达式提取ISBN [英] Extract an ISBN with regex

查看:72
本文介绍了使用正则表达式提取ISBN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常长的字符串,我想将其解析为出现在子字符串"ISBN"之后的数值.但是,这组13位数字可以通过-"字符进行不同的排列.示例:(这些都是有效的ISBN) 123-456-789-123-4 ,或 1-2-3-4-5-67891234 ,或12-34-56-78-91-23-4 .本质上,我想在潜在的ISBN上使用正则表达式模式匹配器,以查看是否存在有效的13位ISBN.如何忽略"-"字符,以便仅对 \ d {13} 模式进行正则表达式?我的功能:

I have an extremely long string that I want to parse for a numeric value that occurs after the substring "ISBN". However, this grouping of 13 digits can be arranged differently via the "-" character. Examples: (these are all valid ISBNs) 123-456-789-123-4, OR 1-2-3-4-5-67891234, OR 12-34-56-78-91-23-4. Essentially, I want to use a regex pattern matcher on the potential ISBN to see if there is a valid 13 digit ISBN. How do I 'ignore' the "-" character so I can just regex for a \d{13} pattern? My function:

public String parseISBN (String sourceCode) {
  int location = sourceCode.indexOf("ISBN") + 5;
  String ISBN = sourceCode.substring(location); //substring after "ISBN" occurs
  int i = 0;
  while ( ISBN.charAt(i) != ' ' )
    i++;
  ISBN = ISBN.substring(0, i); //should contain potential ISBN value
  Pattern pattern = Pattern.compile("\\d{13}"); //this clearly will find 13 consecutive numbers, but I need it to ignore the "-" character
  Matcher matcher = pattern.matcher(ISBN); 
  if (matcher.find()) return ISBN;
  else return null;
}

推荐答案

  • 替代方法1:

    • Alternative 1:

      pattern.matcher(ISBN.replace("-", ""))
      

    • 替代方法2:类似的东西

    • Alternative 2: Something like

      Pattern.compile("(\\d-?){13}")
      

    • 第二个替代项的演示

      String ISBN = "ISBN: 123-456-789-112-3, ISBN: 1234567891123";
      
      Pattern pattern = Pattern.compile("(\\d-?){13}");
      Matcher matcher = pattern.matcher(ISBN);
      
      while (matcher.find())
          System.out.println(matcher.group());
      

      输出:

      123-456-789-112-3
      1234567891123
      

      这篇关于使用正则表达式提取ISBN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆