Reputation: 81
I try to learn groovy in SoapUI and now I have a problem with a regular expression in groovy.
I try build a regular expression in groovy to parse this text:
[EM6111949VA61=cfefcgjnjcidaiieodmlgfijaiknankpiodhghbagndikijiieicfhiiiojmfcfgjjkokkoilinbkkedcaafplikgjhomkhnopjhfhpjgkadalbkeenengiapjpeaiemokenfckjbeho; path=/bla/bla; secure; HttpOnly]
The result must be:
EM6111949VA61=cfefcgjnjcidaiieodmlgfijaiknankpiodhghbagndikijiieicfhiiiojmfcfgjjkokkoilinbkkedcaafplikgjhomkhnopjhfhpjgkadalbkeenengiapjpeaiemokenfckjbeho
My groovy source-code in SoapUI is:
erg='[EM6111949VA61=cfefcgjnjcidaiieodmlgfijaiknankpiodhghbagndikijiieicfhiiiojmfcfgjjkokkoilinbkkedcaafplikgjhomkhnopjhfhpjgkadalbkeenengiapjpeaiemokenfckjbeho; path=/bla/bla; secure; HttpOnly]'
def muster = /(?<=\[)[\w]+=[\w]+/
(erg ==~ muster).each {
log.info "Muster $it"
}
the log.info result is: Muster false
I try the text with the Expression::
(?<=\[)[\w]+=[\w]+ to http://www.regexe.de/
And the result is what I need,
EM6111949VA61=cfefcgjnjcidaiieodmlgfijaiknankpiodhghbagndikijiieicfhiiiojmfcfgjjkokkoilinbkkedcaafplikgjhomkhnopjhfhpjgkadalbkeenengiapjpeaiemokenfckjbeho
What is the correct Syntax in groovy
Thanks a lot for your answers
Michael
Upvotes: 4
Views: 1547
Reputation: 627292
Your pattern works, but the ==~
operator requires the whole string to match this pattern. Your current regex is equal to /^(?<=\[)[\w]+=[\w]+\z/
.
Use =~
that allows partial matches, not ==~
(that requires full string matches).
Note you may write [\w]
as \w
since \w
is the only "atom" in the character class.
See the Groovy demo:
def erg='[EM6111949VA61=cfefcgjnjcidaiieodmlgfijaiknankpiodhghbagndikijiieicfhiiiojmfcfgjjkokkoilinbkkedcaafplikgjhomkhnopjhfhpjgkadalbkeenengiapjpeaiemokenfckjbeho; path=/bla/bla; secure; HttpOnly]'
def regex = /(?<=\[)\w+=\w+/
def muster = erg =~ regex
(0..<muster.count).each { print muster[it] }
Output:
EM6111949VA61=cfefcgjnjcidaiieodmlgfijaiknankpiodhghbagndikijiieicfhiiiojmfcfgjjkokkoilinbkkedcaafplikgjhomkhnopjhfhpjgkadalbkeenengiapjpeaiemokenfckjbeho
Upvotes: 2