- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在从与小说相关的帖子中提取标题。目的是通过使用正则表达式来确定帖子是关于哪一章的。每个站点使用不同的方式来识别章节。以下是最常见的情况:
$title = 'text chapter 25.6 text'; // c25.6
$title = 'text chapters 23, 24, 25 text'; // c23-25
$title = 'text chapters 23+24+25 text'; // c23-25
$title = 'text chapter 23, 25 text'; // c23 & 25
$title = 'text chapter 23 & 24 & 25 text'; // c23-25
$title = 'text c25.5-30 text'; // c25.5-30
$title = 'text c99-c102 text'; // c99-102
$title = 'text chapter 99 - chapter 102 text'; // c99-102
$title = 'text chapter 1 - 3 text'; // c1-3
$title = '33 text chapter 1, 2 text 3'; // c1-2
$title = 'text v2c5-10 text'; // c5-10
$title = 'text chapters 23, 24, 25, 29, 31, 32 text'; // c23-25 & 29 & 31-32
章节编号总是列在标题中,只是在上面显示的不同变体中。
到目前为止,我有一个正则表达式来确定章节的单个案例,例如:
$title = '9 text chapter 25.6 text'; // c25.6
使用此代码(尝试 ideone ):
function get_chapter($text, $terms) {
if (empty($text)) return;
if (empty($terms) || !is_array($terms)) return;
$values = false;
$terms_quoted = array();
foreach ($terms as $term)
$terms_quoted[] = preg_quote($term, '/');
// search for matches in $text
// matches with lowercase, and ignores white spaces...
if (preg_match('/('.implode('|', $terms_quoted).')\s*(\d+(\.\d+)?)/i', $text, $matches)) {
if (!empty($matches[2]) && is_numeric($matches[2])) {
$values = array(
'term' => $matches[1],
'value' => $matches[2]
);
}
}
return $values;
}
$text = '9 text chapter 25.6 text'; // c25.6
$terms = array('chapter', 'chapters');
$chapter = get_chapter($text, $terms);
print_r($chapter);
if ($chapter) {
echo 'Chapter is: c'. $chapter['value'];
}
如何使用上面列出的其他示例进行此操作?鉴于这个问题的复杂性,如果符合条件,我将奖励 200 分。
最佳答案
我建议采用以下结合正则表达式和通用字符串处理逻辑的方法:
preg_match
使用适当的正则表达式来匹配以 $terms
中的关键字开头的整个文本 block 的第一次出现数组,直到与术语相关的最后一个数字(+ 可选部分字母)+
连接的情况下重建数字范围来完成后处理。 , &
或 ,
字符。这需要一个多步骤操作:1)匹配前一个整体匹配中的连字符分隔的子字符串,并修剪掉不必要的零和空格,2)将数字 block 拆分为单独的项目并将它们传递给一个单独的函数,该函数将生成数字范围buildNumChain($arr)
函数将创建数字范围,如果一个字母跟在数字后面,会将其转换为 section X
后缀。你可以使用
$strs = ['c0', 'c0-3', 'c0+3', 'c0 & 9', 'c0001, 2, 03', 'c01-03', 'c1.0 - 2.0', 'chapter 2A Hello', 'chapter 2AHello', 'chapter 10.4c', 'chapter 2B', 'episode 23.000 & 00024', 'episode 23 & 24', 'e23 & 24', 'text c25.6 text', '001 & 2 & 5 & 8-20 & 100 text chapter 25.6 text 98', 'hello 23 & 24', 'ep 1 - 2', 'chapter 1 - chapter 2', 'text chapter 25.6 text', 'text chapters 23, 24, 25 text','text chapter 23, 25 text', 'text chapter 23 & 24 & 25 text','text c25.5-30 text', 'text c99-c102 text', 'text chapter 1 - 3 text', '33 text chapter 1, 2 text 3','text chapters 23, 24, 25, 29, 31, 32 text', 'c19 & c20', 'chapter 25.6 & chapter 29', 'chapter 25+c26', 'chapter 25 + 26 + 27'];
$terms = ['episode', 'chapter', 'ch', 'ep', 'c', 'e', ''];
usort($terms, function($a, $b) {
return strlen($b) - strlen($a);
});
$chapter_main_rx = "\b(?|" . implode("|", array_map(function ($term) {
return strlen($term) > 0 ? "(" . substr($term, 0, 1) . ")(" . substr($term, 1) . "s?)": "()()" ;},
$terms)) . ")\s*";
$chapter_aux_rx = "\b(?:" . implode("|", array_map(function ($term) {
return strlen($term) > 0 ? substr($term, 0, 1) . "(?:" . substr($term, 1) . "s?)": "" ;},
$terms)) . ")\s*";
$reg = "~$chapter_main_rx((\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+-]|and)\s*(?:$chapter_aux_rx)?(?4))*)~ui";
foreach ($strs as $s) {
if (preg_match($reg, $s, $m)) {
$p3 = preg_replace_callback(
"~(\d*(?:\.\d+)?)([A-Z]?)\s*-\s*(?:$chapter_aux_rx)?|(\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?(?1))*~ui", function($x) use ($chapter_aux_rx) {
return (isset($x[3]) && strlen($x[3])) ? buildNumChain(preg_split("~\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?~ui", $x[0]))
: ((isset($x[1]) && strlen($x[1])) ? ($x[1] + 0) : "") . ((isset($x[2]) && strlen($x[2])) ? ord(strtolower($x[2])) - 96 : "") . "-";
}, $m[3]);
print_r(["original" => $s, "found_match" => trim($m[0]), "converted" => $m[1] . $p3]);
echo "\n";
} else {
echo "No match for '$s'!\n";
}
}
function buildNumChain($arr) {
$ret = "";
$rngnum = "";
for ($i=0; $i < count($arr); $i++) {
$val = $arr[$i];
$part = "";
if (preg_match('~^(\d+(?:\.\d+)?)([A-Z]?)$~i', $val, $ms)) {
$val = $ms[1];
if (!empty($ms[2])) {
$part = ' part ' . (ord(strtolower($ms[2])) - 96);
}
}
$val = $val + 0;
if (($i < count($arr) - 1) && $val == ($arr[$i+1] + 0) - 1) {
if (empty($rngnum)) {
$ret .= ($i == 0 ? "" : " & ") . $val;
}
$rngnum = $val;
} else if (!empty($rngnum) || $i == count($arr)) {
$ret .= '-' . $val;
$rngnum = "";
} else {
$ret .= ($i == 0 ? "" : " & ") . $val . $part;
}
}
return $ret;
}
见 PHP demo .
c
或 chapter
/chapters
后面有数字,只捕获 c
和数字<number>-c?<number>
子字符串应该去掉空格和 c
之前/之间的数字和,
/&
- 分隔的数字应使用 buildNumChain
进行后处理从连续数字中生成范围(假定为整数)。如果$terms = ['episode', 'chapter', 'ch', 'ep', 'c', 'e', '']
,主正则表达式将如下所示:
'~(?|(e)(pisodes?)|(c)(hapters?)|(c)(hs?)|(e)(ps?)|(c)(s?)|(e)(s?)|()())\s*((\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+-]|and)\s*(?:(?:e(?:pisodes?)|c(?:hapters?)|c(?:hs?)|e(?:ps?)|c(?:s?)|e(?:s?)|)\s*)?(?4))*)~ui'
见 regex demo .
模式详情
(?|(e)(pisodes?)|(c)(hapters?)|(c)(hs?)|(e)(ps?)|(c)(s?)|(e)(s?)|()())
- 一个分支重置组,它捕获搜索词的第一个字母并将剩余的词捕获到强制组 2。如果有一个空词,()()
添加以确保组中的分支包含相同数量的组\s*
- 0+ 个空格((\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+-]|and)\s*c?(?3))*)
- 第 2 组:
(\d+(?:\.\d+)?(?:[A-Z]\b)?)
- 第 3 组:1+ 位,后跟 .
的可选序列, 1+ 个数字,然后是一个可选的 ASCII 字母,后跟一个非单词字符或字符串结尾(注意不区分大小写的修饰符将使 [A-Z]
也匹配小写 ASCII 字母)(?:\s*(?:[,&+-]|and)\s*(?:(?:e(?:pisodes?)|c(?:hapters?)|c(?:hs?)|e(?:ps?)|c(?:s?)|e(?:s?)|)\s*)?(?4))*
- 零个或多个序列
\s*(?:[,&+-]|and)\s*
- 一个 ,
, &
, +
, -
或 and
包含可选的 0+ 个空格(?:e(?:pisodes?)|c(?:hapters?)|c(?:hs?)|e(?:ps?)|c(?:s?)|e(?:s?)|)
- 添加可选复数结尾的任何术语s
(?4)
- 第 4 组模式递归/重复当正则表达式匹配时,组 1 的值为 c
,所以这将是结果的第一部分。那么,
"~(\d*(?:\.\d+)?)([A-Z]?)\s*-\s*(?:$chapter_aux_rx)?|(\d+(?:\.\d+)?(?:[A-Z]\b)?)(?:\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?(?1))*~ui"
在preg_replace_callback
内部使用删除 -
之间的空格(如果有)和术语(如果有)后跟 0+ 个空白字符,如果第 1 组匹配,则匹配以
"~\s*(?:[,&+]|and)\s*(?:$chapter_aux_rx)?~ui"
正则表达式(它匹配 &
、 ,
、 +
或 and
在可选的 0+ 空格后跟 0+ 空格,然后是可选字符串,术语后跟 0+ 空格)并且数组是传递给 buildNumChain
构建结果字符串的函数。
关于php - 确定不同类型文本中的章节编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51362947/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!