我必须在花括号之前提取方括号和50个单词之间的数字。
Input I am very much new to java...[1] and i want to know....[1-2] more.
I am
a student..[3]
output [1] I am very much new to java...
[1-2] and i want to know...
[3] I am a student...
please help me.
答案 0 :(得分:0)
这听起来很像家庭作业,我不想为你解决。但这是一般的想法:
[bracket expressions]
。有任何问题吗? :)
答案 1 :(得分:0)
您可以使用正则表达式
((?:\w+\W*?){0,50}?)\[([\d-]+)\]
它捕获捕获组2中方括号中的数字,以及捕获组1中前面的文本。
( // first capture group
(?:
\w+ // consume a sequence of word characters
\W*? // and any following non-word characters, if any
){0,50}? // up to 50 words, as few as possible.
)
\[ // a square opening bracket
( // second capture group
[\d-]+ // a sequence of digits and dashes
)
\] // a square closing bracket