Groovy 中 SOAP 响应的正则表达式
Regular Expression for SOAP Response in Groovy
我在 matching/extracting UserToken 的值(即 "bb14MY")上使用 Groovy 中的正则表达式做错了吗?
@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*
import wslite.http.auth.*
import java.util.regex.*
import groovy.xml.*
import groovy.util.*
import java.lang.*
...
...
...
def soapResponse = connection.content.text;
String str = println "$soapResponse";
Pattern pattern = Pattern.compile("^\s*UserToken="(.*?)"", Pattern.DOTALL);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
println(matcher.group(1));
}
$soapResponse 的输出如下所示。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
ContactAddress_Key="" ImageFile="" LocaleCode="en_US_EST"
UserToken="bb14MY"
</loginReturn></p561:loginResponse></soapenv:Body></soapenv:Envelope>
使用以下模式获取您的用户令牌。我正在使用捕获组来获取它。
UserToken=.+;(\w+).+;
演示 here.
不用说,您的正则表达式对象必须处理多行。
这里使用 ^
是错误的,因为 DOTALL 只是使换行符与常规字符匹配。我想你想要 MULTILINE,但它也可以在没有它的情况下工作。例如
def soap="""\
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
ContactAddress_Key="" ImageFile="" LocaleCode="en_US_EST"
UserToken="bb14MY"
</loginReturn></p561:loginResponse></soapenv:Body></soapenv:Envelope>"""
def pattern
def matcher
// stick with `Pattern.DOTALL` (`(?s)`), but get rid of handling for the start of the line:
pattern = /(?s)UserToken="(.*?)"/
matcher = soap =~ pattern
assert matcher
assert matcher.group(1)=="bb14MY"
// or use `Pattern.MULTILINE` (`(?m)`) with your `^\s*`:
pattern = /(?m)^\s*UserToken="(.*?)"/
matcher = soap =~ pattern
assert matcher
assert matcher.group(1)=="bb14MY"
def soapResponse = connection.content.text;
Pattern pattern = Pattern.compile(/(?s)UserToken="(.*?)"/, Pattern.DOTALL);
Matcher matcher = pattern.matcher(soapResponse);
if (matcher.find()) {
println(matcher.group(1));
}
这就是我解决问题的方法。
我在 matching/extracting UserToken 的值(即 "bb14MY")上使用 Groovy 中的正则表达式做错了吗?
@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='1.1.0')
import wslite.soap.*
import wslite.http.auth.*
import java.util.regex.*
import groovy.xml.*
import groovy.util.*
import java.lang.*
...
...
...
def soapResponse = connection.content.text;
String str = println "$soapResponse";
Pattern pattern = Pattern.compile("^\s*UserToken="(.*?)"", Pattern.DOTALL);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
println(matcher.group(1));
}
$soapResponse 的输出如下所示。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
ContactAddress_Key="" ImageFile="" LocaleCode="en_US_EST"
UserToken="bb14MY"
</loginReturn></p561:loginResponse></soapenv:Body></soapenv:Envelope>
使用以下模式获取您的用户令牌。我正在使用捕获组来获取它。
UserToken=.+;(\w+).+;
演示 here.
不用说,您的正则表达式对象必须处理多行。
这里使用 ^
是错误的,因为 DOTALL 只是使换行符与常规字符匹配。我想你想要 MULTILINE,但它也可以在没有它的情况下工作。例如
def soap="""\
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
ContactAddress_Key="" ImageFile="" LocaleCode="en_US_EST"
UserToken="bb14MY"
</loginReturn></p561:loginResponse></soapenv:Body></soapenv:Envelope>"""
def pattern
def matcher
// stick with `Pattern.DOTALL` (`(?s)`), but get rid of handling for the start of the line:
pattern = /(?s)UserToken="(.*?)"/
matcher = soap =~ pattern
assert matcher
assert matcher.group(1)=="bb14MY"
// or use `Pattern.MULTILINE` (`(?m)`) with your `^\s*`:
pattern = /(?m)^\s*UserToken="(.*?)"/
matcher = soap =~ pattern
assert matcher
assert matcher.group(1)=="bb14MY"
def soapResponse = connection.content.text;
Pattern pattern = Pattern.compile(/(?s)UserToken="(.*?)"/, Pattern.DOTALL);
Matcher matcher = pattern.matcher(soapResponse);
if (matcher.find()) {
println(matcher.group(1));
}
这就是我解决问题的方法。