Get trailing text and create a string

Hi All,

I have a long .txt file that I am reading in Katalon studio. Lets say its 100 lines of mostly random text.
File myLog = new File(“C:\file.txt”)
String myLogText = FileUtils.readFileToString(myLog, StandardCharsets.UTF_8)

I want to find a create a string that contains ‘error12345’
the text ‘error’ is constant and I can find it with myLogText.contains(‘error’)

But the following numbers are random, so how do I find and create the string with both the constant text and random numbers that follow ?


You can try to achieve this by a clever regex, this may help:

Otherwise, when you have to iterate over the text file, once you know the current line containing ‘error’ extract that and append to it the value at it +1

@svhm if you are looking for any line that contains the word error and a random set of numbers afterwards, I suggest you look at the first character after the numbers and then use substring to cut the string before that character (You can also use a blank space as a character).

For example, in your log file it could look like:

error12345: Error is caused by blah blah…

You can then cut the string at the character :

Here is the code:

String errorFileText = 'error12345: Error is caused by blahblah'

String error = errorFileText.substring(errorFileText.indexOf('error'), errorFileText.indexOf(':'))

Note that the string only returned the error with the number, but this will always return the first instance of error found

If you are still struggling, please send through an example of an error line.

@bionel This is very close, how would I apply this to my scenario ?
I just want to find the word ‘error’ and any trailing text, even if its just the rest of the line.

@jmeintjesn7 thanks for the reply but this is more the next step, as your example knows what the text is already that trails the word ‘error’. Im trying to find out what the text is, that trails the word ‘error’, as it cannot be anticipated.
Even if I could grab the entire line that the word error is on, that would be good too.

Then I will move onto substringing it to clean it up.

Thanks @bionel for sending me in the right direction, I found the solution on another forum.

So in my txt file there is a line that contains error12345, but I dont actually know what the numbers will be, could be 54321 or 15243 etc etc

String logText = FileUtils.readFileToString(Log, StandardCharsets.UTF_8)

Pattern p = Pattern.compile( “error(.*)”)
Matcher m = p.matcher(logText)

if (m.find()) {
String s =

The result prints