Scala在一个字符串中查找另一个字符串的位置

5

我有一个字符串:

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">
                     &nbsp;
                </td>
                <td class="DataColTotal">
                    <!--x_amount.val-->
                    US&nbsp;$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>

我想要找到字符串中"x_auth_code.1.val"的位置。然后,我想从该位置开始获取一定数量的字符来获得授权码。目标是返回授权码。


哇,那行代码真的需要拆分成多行。 - Alex Wilson
相关:https://dev59.com/X3I-5IYBdhLWcg3wq6do - Luigi Plinge
2个回答

17

您可以使用indexOfSlice,然后在StringOps中使用slice()

scala> val myString = "Hello World!"
myString: java.lang.String = Hello World!

scala> val index = myString.indexOfSlice("Wo")
index: Int = 6

scala> val slice = myString.slice(index, index+5)
slice: String = World

使用您的HTML字符串:

scala> htmlString.indexOfSlice("x_auth_code.1.val")
res4: Int = 2771

indexOfSlice 是工具套件中很好的补充,因为它的最坏情况 BigO 运行时复杂度很低。 - Paul

0

为什么不使用XML解析器?不要将XML视为字符串--如果这样做,你会受到伤害。

这里有一个正则表达式可以做到,但我的建议是:不要使用它!使用XML工具。

"""\Qx_auth_code.1.val\E[^>]*>([^<]*)""".r.findFirstMatchIn(htmlString).map(_ group 1)

5
他没有使用XML解析器,因为他没有处理XML。这是HTML 4.0过渡性版本。当然有针对它的解析器,但它可能仍然存在格式问题。所以如果你想推荐一个解析器,也许像jsoup这样的工具会很好。但是如果他只需要提取一个字符串,那么这样做就过于复杂了。 - Kim Stebel
@KimStebel 有很多工具可以处理这种情况。例如,Tag SoupJTidy - Daniel C. Sobral
是的,它们都可能过于复杂了。 - Kim Stebel
@KimStebel TagSoup是一个轻量级的SAX解析器,因此它比标准的SAX解析器更加精简。 - Daniel C. Sobral
我所说的“过度设计”是指它是多余的依赖项,可能并没有简化他的代码。 - Kim Stebel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接