正则表达式从数据中识别字符串和数字

时间:2015-07-06 04:59:53

标签: regex pcre

Jhon Wick是我的名字,我每天花50美元

我需要50美元的人的姓氏 我做了一个正则表达式来识别每个人名字后面的姓名。

这是它的正则表达式 的 ^ \ W + \ S +(\ W +)。+ $

但我还要检索50美元。

所以,请帮我制作这种正则表达式

3 个答案:

答案 0 :(得分:1)

试试这个:

^\w+\s+(\w+).+?(\d+\$)

答案 1 :(得分:1)

所有美元比赛。

带有全局标记的

^\w+\s+(\w+)|(\d+\$)

DEMO

您的想法是,您需要两个单独的匹配,就像在某些情况下使用与行相关的字符^$,甚至是*+时一样,你很可能只获得第一场或最后一场比赛。

获得名称结果后,正则表达式只会查找所有美元值,而不会查看其上下文。

答案 2 :(得分:0)

试试这个它也适用于三个或更多单词名称:

<h2>Shopping Card Example</h2>
<div ng:controller="CartForm">
    <table class="table">
        <tr>

            <th>Description</th>
            <th>Qty</th>
            <th>Cost</th>
            <th>Total</th>
            <th></th>
        </tr>
        <tr ng:repeat="item in invoice.items">
            <td>{{item.description}}</td>           
            <td>{{item.qty}}</td>
            <td>{{item.cost}}</td>
            <td>{{item.qty * item.cost | currency}}</td>
            <td>
                [<a href ng:click="removeItem($index)">X</a>]
            </td>
        </tr>
        <tr>
           <td><input type="text" ng:model="item.description"class="input-small"></td>           
            <td><input type="number" ng:model="item.qty" ng:required class="input-mini"></td>
            <td><input type="number" ng:model="item.cost" ng:required class="input-mini"></td>
            <td>{{item.qty * item.cost | currency}}</td>
            <td>
                [<a href ng:click="removeItem($index)">X</a>]
            </td>
        </tr>

        <tr>
            <td><a href ng:click="addItem()" class="btn btn-small">add item</a></td>
            <td></td>
            <td>Total:</td>
            <td>{{total() | currency}}</td>
        </tr>
    </table>
</div>