Extract an ISBN with regex

后端 未结 7 1866
终归单人心
终归单人心 2021-01-14 01:01

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 differ

相关标签:
7条回答
  • 2021-01-14 01:06

    You can strip out the dashes with string manipulation, or you could use this:

    "\\b(?:\\d-?){13}\\b"
    

    It has the added bonus of making sure the string doesn't start or end with -.

    0 讨论(0)
  • 2021-01-14 01:16

    Try this:

    Pattern.compile("\\d(-?\\d){12}")
    
    0 讨论(0)
  • 2021-01-14 01:16

    Try stripping the dashes out, and regex the new string

    0 讨论(0)
  • 2021-01-14 01:27

    Do it in one step with a pattern recognizing everything, and optional dashes between digits. No need to fiddle with ISBN offset + substrings.

    ISBN(\d(-?\d){12})
    

    If you want the raw number, strip dashes from the first matched subgroup afterwards. I am not a Java guy so I won't show you code.

    0 讨论(0)
  • 2021-01-14 01:28

    Use this pattern:

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

    and strip all dashes from the found isbn number

    0 讨论(0)
  • 2021-01-14 01:29
    • Alternative 1:

      pattern.matcher(ISBN.replace("-", ""))
      
    • Alternative 2: Something like

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

    Demo of second alternative:

    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());
    

    Output:

    123-456-789-112-3
    1234567891123
    
    0 讨论(0)
提交回复
热议问题