Charlie Zeng
Charlie Zeng

Reputation: 131

How to use regular expression to locate certain string B after certain string A across multiple lines separeted by "\n"

I have text stored in a string from a pdf file. And there are around 30 lines separated by "\n". And I am using regex in java to locate a substring behind another string. But these two strings are not in the same line.

Ex, text like this:

Title
content1
the percentage is 23%
This is a test. Test A
the percentage is 80%
content2

I need to get all percentages that are after "This is a test", in this case 80%.

I was using regex: (?<=This is a test.)\\d*\\d\\%

But seems it's only looking into the line "This is a test. Test A", not anything after that.

I know I can use the split but in real case I might need to split multiple times and that will comprmise the current method sturcture I have right now. Trying to accomplish it using pure regex. Please give me some advice.

Upvotes: 1

Views: 38

Answers (1)

vks
vks

Reputation: 67988

This is a test[\s\S]*?(\d+%)

Try this.Grab the capture or group.See demo.

https://regex101.com/r/pM9yO9/11

For java use

This is a test[\\s\\S]*?(\\d+%)

Upvotes: 1

Related Questions