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 ?
@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
@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.