正则表达式-从书名中提取卷和章号 [英] Regex - Extracting volume and chapter numbers from book titles

查看:141
本文介绍了正则表达式-从书名中提取卷和章号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,
我正在尝试将一些旧数据导入一个全新的系统中,这几乎完成了,但是有一个很大的问题!假设这些数据:

Blabla Vol.1 chapter 2
ABCD in the era of XYZ volume 2 First Chapter  
A really useless book Eighth vol  
Blala Sixth Vol Chapter 5  
Lablah V6C7 2002  
FooBar Vol6 C3 by Dr. Foo Bar
Regex: A tool in Hell V1 Eleventh Chapter

困惑!!我试图编写该正则表达式来提取卷和章号,但是您知道它是正则表达式!有人可以指导我吗?

解决方案

以下是与您的示例匹配的正则表达式:

/^.+?(?|(?:\bVol.?|\bvolume[ ]+|V)(\d+)|[ ]+([a-z]+)[ ]+vol\b).?(?:(?|(?:C|chapter[ ]+)(\d+)|[ ]+([a-z]+)[ ]+Chapter\b).?)?$/im

您可以在此处实时编辑正则表达式和/或添加测试.. /p>

在此链接:
数组中的

  • 元素[0]表示匹配数组
  • 元素[1]卷数组
  • 元素[2]章节数组

  • 我假设您的示例中所述的 卷总是在 章之前.

    Hey,
    I'm trying to import some legacy data into a brand new system, it's almost done, but there's a huge problem! Assuming these kinda data:

    Blabla Vol.1 chapter 2
    ABCD in the era of XYZ volume 2 First Chapter  
    A really useless book Eighth vol  
    Blala Sixth Vol Chapter 5  
    Lablah V6C7 2002  
    FooBar Vol6 C3 by Dr. Foo Bar
    Regex: A tool in Hell V1 Eleventh Chapter
    

    Confused!! I tried to write that regex to extract volume and chapter numbers but you know it's REGEX! Can anyone please guide me through this?

    解决方案

    Here is a regular expression that will match your example :

    /^.+?(?|(?:\bVol.?|\bvolume[ ]+|V)(\d+)|[ ]+([a-z]+)[ ]+vol\b).?(?:(?|(?:C|chapter[ ]+)(\d+)|[ ]+([a-z]+)[ ]+Chapter\b).?)?$/im

    You can live edit the regex and/or add tests here.

    In this link :

  • element [0] in the array refers to the matches array
  • element [1] the volumes array
  • element [2] the chapter array

  • I assumed that volumes always comes before chapters as stated in your examples.

    这篇关于正则表达式-从书名中提取卷和章号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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