本文介绍了正则表达式仅提取字符串之后和空格之前的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在给定字符串后匹配文本.在这种情况下,以BookTitle"开头但在第一个空格之前的行的文本:
I want to match text after given string. In this case, the text for lines starting with "BookTitle" but before first space:
BookTitle:HarryPotter JK Rowling
BookTitle:HungerGames Suzanne Collins
Author:StephenieMeyer BookTitle:Twilight
所需的输出是:
HarryPotter
HungerGames
我试过:"^BookTitle(.*)"
但它给了我匹配 BookTitle: 在行中间的地方,以及空格之后的所有东西.有人帮忙吗?
I tried: "^BookTitle(.*)"
but it's giving me matches where BookTitle: is in middle of line, and also all the stuff after white space. Anyone help?
推荐答案
你可以在你的模式中使用 positive lookbehind
.
you can have positive lookbehind
in your pattern.
(?<=BookTitle:).*?(?=\s)
更多信息:前瞻和后视零宽度断言
这篇关于正则表达式仅提取字符串之后和空格之前的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!