Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

Finding in-between 2 consecutive numbers (regEx)

I have bee trying to create a exam paper parser. I am struggling to make a regular expression for capturing the question in-between 2 consecutive numbers. Here is a sample of the paper:

21 (a) (i) Protons Neutrons Electrons
29 Si 14
1
22 (a) (i) Oxidised

I would like to capture the 21 and (a) (i) Protons Neutrons Electrons 29 Si 14 16 14 1 in this example.

Here is my attempt https://regex101.com/r/dlESTV/3 I am finding this very difficult, because the expression will capture everything in-between the 21 and 29 (instead of 22)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use

^(d+) (.*(?:
?
(?!d+ +([a-z]+)).*)*)

See the regex demo.

Details:

  • ^ - start of a line
  • (d+) - Group 1: one or more digits
  • - a space (you may add + after the space, but it is only necessary if you need to make sure there is a ( after it)
  • (.*(?: ? (?!d+ +([a-z]+)).*)*) - Group 1:
    • .* - the rest of the line
    • (?: ? (?!d+ +([a-z]+)).*)* - zero or more lines that do not start with
      • d+ - one or more digits
      • + - one or more spaces
      • ([a-z]+) - a (, then one or more lowercase ASCII letters, then ).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...