I have a 2 string. first that has quots «text» and text ,
the second that has quots «text» and other text . How can I write a regex to extract «the data i want »from the following texts?
"«(.*?)» "a regular expression that should work
Sorry for my English
Please use Code Formatting Markdown syntax for better presenting your code.
/* variable in which the text of the format "«text» text...." */
header = Mobile.getText(findTestObject('Object Repository/mainNews/header'), 0)
Mobile.tap(findTestObject('Object Repository/mainNews/header'), 0)
/* variable in which the text of the format "«text» other text" */
header1 = Mobile.getText(findTestObject('mainNews/h1'), 0)
Mobile.verifyMatch(header1, header, true)
Do you mean you want to extract a text enclosed by a pair of «
and »
character?
exactly."«(.*?)» " regular expression . but I don’t know how to apply it
See https://www.baeldung.com/groovy-pattern-matching for using Regex in Groovy.
def header = "«text» text...."
def matcher = header =~ /«(.*)»/
assert matcher.size() == 1
println matcher[0][1]
def header1 = "«TEXT» other text"
def matcher1 = header1 =~ /«(.*)»/
assert matcher.size() == 1
println matcher1[0][1]
When I ran it, I saw
2021-10-22 17:44:45.521 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2021-10-22 17:44:45.524 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/New Test Case (3)
2021-10-22 17:44:46.339 DEBUG testcase.New Test Case (3) - 1: header = "«text» text...."
2021-10-22 17:44:46.341 DEBUG testcase.New Test Case (3) - 2: matcher = header =~ "«(.*)»"
2021-10-22 17:44:46.343 DEBUG testcase.New Test Case (3) - 3: assert matcher.size() == 1
2021-10-22 17:44:46.354 DEBUG testcase.New Test Case (3) - 4: println(matcher[0][1])
text
2021-10-22 17:44:46.368 DEBUG testcase.New Test Case (3) - 5: header1 = "«TEXT» other text"
2021-10-22 17:44:46.370 DEBUG testcase.New Test Case (3) - 6: matcher1 = header1 =~ "«(.*)»"
2021-10-22 17:44:46.372 DEBUG testcase.New Test Case (3) - 7: assert matcher.size() == 1
2021-10-22 17:44:46.374 DEBUG testcase.New Test Case (3) - 8: println(matcher1[0][1])
TEXT
2021-10-22 17:44:46.391 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/New Test Case (3)
I wonder if «(.*?)»
is a valid regexp.
I used «(.*)»
instead.
Thank you very much, this is not the first time I have used your advice from the forum. you are awesome
will this work with another regex?
example " (^.{35}) "
I don’t understand your question.
Why not you just try and see?