String Scala查找字符串中字符串的位置
我有这个字符串:String Scala查找字符串中字符串的位置,string,scala,String,Scala,我有这个字符串: var htmlString; 分配给: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <title>Payment Receipt</title> <link rel="stylesheet" type="text/css" href="content/PaymentForm.css"> <
var htmlString;
分配给:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Payment Receipt</title>
<link rel="stylesheet" type="text/css" href="content/PaymentForm.css">
<style type="text/css">
</style>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
</head>
<body>
<div id="divPageOuter" class="PageOuter">
<div id="divPage" class="Page">
<!--[1]-->
<div id="divThankYou">
Thank you for your order!
</div>
<hr class="HrTop">
<div id="divReceiptMsg">
You may print this receipt page for your records.
</div>
<div class="SectionBar">
Order Information
</div>
<table id="tablePaymentDetails1Rcpt">
<tr>
<td class="LabelColInfo1R">
Merchant:
</td>
<td class="DataColInfo1R">
<!--Merchant.val-->
Ryan
<!--end-->
</td>
</tr>
<tr>
<td class="LabelColInfo1R">
Description:
</td>
<td class="DataColInfo1R">
<!--x_description.val-->
Rasmussenpayment
<!--end-->
</td>
</tr>
</table>
<table id="tablePaymentDetails2Rcpt" cellspacing="0" cellpadding="0">
<tr>
<td id="tdPaymentDetails2Rcpt1">
<table>
<tr>
<td class="LabelColInfo1R">
Date/Time:
</td>
<td class="DataColInfo1R">
<!--Date/Time.val-->
09-Jul-2012 12:26:46 PM PT
<!--end-->
</td>
</tr>
<tr>
<td class="LabelColInfo1R">
Customer ID:
</td>
<td class="DataColInfo1R">
<!--x_cust_id.val-->
<!--end-->
</td>
</tr>
</table>
</td>
<td id="tdPaymentDetails2Rcpt2">
<table>
<tr>
<td class="LabelColInfo1R">
Invoice Number:
</td>
<td class="DataColInfo1R">
<!--x_invoice_num.val-->
176966244
<!--end-->
</td>
</tr>
</table>
</td>
</tr>
</table>
<hr id="hrBillingShippingBefore">
<table id="tableBillingShipping">
<tr>
<td id="tdBillingInformation">
<div class="Label">
Billing Information
</div>
<div id="divBillingInformation">
Test14 Rasmussen<br>
1234 test st<br>
San Diego, CA 92107 <br>
</div>
</td>
<td id="tdShippingInformation">
<div class="Label">
Shipping Information
</div>
<div id="divShippingInformation">
</div>
</td>
</tr>
</table>
<hr id="hrBillingShippingAfter">
<div id="divOrderDetailsBottomR">
<table id="tableOrderDetailsBottom">
<tr>
<td class="LabelColTotal">
Total:
</td>
<td class="DescrColTotal">
</td>
<td class="DataColTotal">
<!--x_amount.val-->
US $250.00
<!--end-->
</td>
</tr>
</table>
<!-- tableOrderDetailsBottom -->
</div>
<div id="divOrderDetailsBottomSpacerR">
</div>
<div class="SectionBar">
Visa ****0027
</div>
<table class="PaymentSectionTable" cellspacing="0" cellpadding="0">
<tr>
<td class="PaymentSection1">
<table>
<tr>
<td class="LabelColInfo2R">
Date/Time:
</td>
<td class="DataColInfo2R">
<!--Date/Time.1.val-->
09-Jul-2012 12:26:46 PM PT
<!--end-->
</td>
</tr>
<tr>
<td class="LabelColInfo2R">
Transaction ID:
</td>
<td class="DataColInfo2R">
<!--Transaction ID.1.val-->
2173493354
<!--end-->
</td>
</tr>
<tr>
<td class="LabelColInfo2R">
Authorization Code:
</td>
<td class="DataColInfo2R">
<!--x_auth_code.1.val-->
07I3DH
<!--end-->
</td>
</tr>
<tr>
<td class="LabelColInfo2R">
Payment Method:
</td>
<td class="DataColInfo2R">
<!--x_method.1.val-->
Visa ****0027
<!--end-->
</td>
</tr>
</table>
</td>
<td class="PaymentSection2">
<table>
</table>
</td>
</tr>
</table>
<div class="PaymentSectionSpacer">
</div>
</div>
<!-- entire BODY -->
</div>
<div class="PageAfter">
</div>
</body>
</html>
付款收据
谢谢您的订单!
您可以打印此收据页以用于记录。
订单信息
商户:
赖安
说明:
拉斯穆森付款
日期/时间:
2012年7月9日12:26:46下午太平洋时间
客户编号:
发票号码:
176966244
计费信息
Test14拉斯穆森
1234测试st
加利福尼亚州圣地亚哥92107
装运信息
总数:
250美元
签证****0027
日期/时间:
2012年7月9日12:26:46下午太平洋时间
事务ID:
2173493354
授权代码:
07I3DH
付款方式:
签证****0027
我想在字符串中找到
“x_auth_code.1.val”
的位置。然后我想从这个位置获得一个字符串加上一定数量的字符。目标是返回授权代码。您可以使用indexOfSlice
,然后在中使用slice()
使用html字符串:
scala> htmlString.indexOfSlice("x_auth_code.1.val")
res4: Int = 2771
为什么不使用XML解析器?不要将XML视为字符串——如果这样做,您会被咬 这里有一个正则表达式,但我的建议是:不要使用它!使用xml工具
"""\Qx_auth_code.1.val\E[^>]*>([^<]*)""".r.findFirstMatchIn(htmlString).map(_ group 1)
“”\Qx\u auth\u code.1.val\E[^>]*>([^Wow.这一行可能真的需要被分成多行。相关:他没有使用XML解析器,因为他没有处理XML。这是HTML 4.0的过渡版。当然有解析器,但它的格式可能仍然不正确。因此,如果你想推荐一个解析器,也许像jsoup这样的东西会很好。但是如果他真的不需要的话ds来提取一个字符串,这是过度杀戮。@KimStebel有很多工具可以处理这种情况。例如,and。是的,它们可能都是过度杀戮。@KimStebel TagSoup是一个轻量级SAX解析器,因此它比标准SAX解析器的过度杀戮要少。我所说的过度杀戮是指多了一个依赖项,可能不会简化他的代码。索引OfSlice是工具套件的一个很好的补充,因为它最糟糕的情况是BigO运行时的复杂性
"""\Qx_auth_code.1.val\E[^>]*>([^<]*)""".r.findFirstMatchIn(htmlString).map(_ group 1)