我在此路径中有一个文件
D:\ebook\comic\Author,_Title_of_Book_(http://google.com).cbz
我需要使用正则表达式分隔字符串并获取作者,标题和URL。 文本输出应为
Author
Title_of_Book
http://google.com
到目前为止我已经
了([^\\]+)\.[^\\]+$
这将只给我文件。
答案 0 :(得分:2)
^[^,]*\\([^,]*),_([^(]*)_\(([^)]*)\)
此正则表达式将执行以下操作:
现场演示
https://regex101.com/r/aB8zQ6/1
示例文字
D:\ebook\comic\Author,_Title_of_Book_(http://google.com).cbz
样本匹配
MATCH 1
1. [15-21] `Author`
2. [23-36] `Title_of_Book`
3. [38-55] `http://google.com`
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
[^,]* any character except: ',' (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
\\ '\'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^,]* any character except: ',' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
,_ ',_'
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[^(]* any character except: '(' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
_ '_'
----------------------------------------------------------------------
\( '('
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[^)]* any character except: ')' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
\) ')'
----------------------------------------------------------------------
答案 1 :(得分:0)
UPDATE @RoYoMi发布了一个非常优秀的答案,但是由于我已经完成了这项工作,所以你走了。
由于您只在此处标记了正则表达式,因此我不知道您需要使用哪种语言才能使用。我为此示例进行了JSFiddle测试。这是代码。
<p>
<input type="text" id="val" value="D:\ebook\comic\AuthorLastName_FirstName,Title_of_Book_(http://google.com).cbz" style="width:100%;" />
</p>
<div id="name"></div>
<div id="title"></div>
<div id="url"></div>
<script>
var val = document.getElementById("val").value;
var name = /\w+(?=[,])/.exec(val).toString().replace(/[_]/g, " ");
var title = /\w+(?=[(])/.exec(val).toString().replace(/[_]/g, " ");
var url = /((http)(s?)[:](\/\/))(.*?)(?=[)])/i.exec(val)[0].toString();
//Better suited with a look behind like this:
//var url = /(?<=[(])(.*?)(?=[)])/.exec(val).toString();
//But JS doesn't support look behinds.
document.getElementById("name").innerHTML = name;
document.getElementById("title").innerHTML = title;
document.getElementById("url").innerHTML = url;
</script>