如何匹配java中的参数列表与正则表达式

时间:2017-06-20 17:17:27

标签: java regex

我需要匹配以下列表:

<div class="AccountInfo">
  <p class="AccountInfo-name">Joe Doe</p>
  <p>
    <!-- react-text: 5 -->IBAN:
    <!-- /react-text -->
    <!-- react-text: 13 -->HTB0001234567
    <!-- /react-text --><br>
    <!-- react-text: 7 -->Balance:
    <!-- /react-text -->
    <!-- react-text: 14 -->3133.56
    <!-- /react-text --><br>
    <!-- react-text: 9 -->Currency:
    <!-- /react-text -->
    <!-- react-text: 10 -->EURO
    <!-- /react-text --><br></p>
</div>
<div class="DebitsAndCredits">
  <div class="DebitsAndCreditsItem">
    <div class="DebitsAndCreditsItem-name">
      <!-- react-text: 17 -->Wendy
      <!-- /react-text -->
      <!-- react-text: 18 -->,
      <!-- /react-text -->
      <!-- react-text: 19 -->
      <!-- /react-text --><span class="text-success">+10.5</span></div><span class="text-muted">Diner</span><br><span class="text-muted">10 January 2016, 12:20 pm</span><br></div>
  <div class="DebitsAndCreditsItem">
    <div class="DebitsAndCreditsItem-name">
      <!-- react-text: 27 -->Danny
      <!-- /react-text -->
      <!-- react-text: 28 -->,
      <!-- /react-text -->
      <!-- react-text: 29 -->
      <!-- /react-text --><span class="text-success">+10.5</span></div><span class="text-muted">Diner</span><br><span class="text-muted">10 January 2016, 12:14 pm</span><br></div>
  <div class="DebitsAndCreditsItem">
    <div class="DebitsAndCreditsItem-name">
      <!-- react-text: 37 -->Joe's Pizza
      <!-- /react-text -->
      <!-- react-text: 38 -->,
      <!-- /react-text -->
      <!-- react-text: 39 -->
      <!-- /react-text --><span class="">-31.5</span></div><span class="text-muted">134678943.88</span><br><span class="text-muted">10 January 2016, 1:23 am</span><br></div>
  <div class="DebitsAndCreditsItem">
    <div class="DebitsAndCreditsItem-name">
      <!-- react-text: 47 -->Northwind Industries
      <!-- /react-text -->
      <!-- react-text: 48 -->,
      <!-- /react-text -->
      <!-- react-text: 49 -->
      <!-- /react-text --><span class="text-success">+2310.7</span></div><span class="text-muted">Salary January</span><br><span class="text-muted">9 January 2016, 7:00 pm</span><br></div>
  <div class="DebitsAndCreditsItem">
    <div class="DebitsAndCreditsItem-name">
      <!-- react-text: 57 -->Coffee and Cakes
      <!-- /react-text -->
      <!-- react-text: 58 -->,
      <!-- /react-text -->
      <!-- react-text: 59 -->
      <!-- /react-text --><span class="">-2.5</span></div><span class="text-muted">468832.99</span><br><span class="text-muted">8 January 2016, 11:14 am</span><br></div>
  <div class="DebitsAndCreditsItem">
    <div class="DebitsAndCreditsItem-name">
      <!-- react-text: 67 -->Albert Heijn
      <!-- /react-text -->
      <!-- react-text: 68 -->,
      <!-- /react-text -->
      <!-- react-text: 69 -->
      <!-- /react-text --><span class="">-76.65</span></div><span class="text-muted">489923982.45</span><br><span class="text-muted">7 January 2016, 10:30 pm</span><br></div>
  <div class="DebitsAndCreditsItem">
    <div class="DebitsAndCreditsItem-name">
      <!-- react-text: 77 -->Shoes and Jackets
      <!-- /react-text -->
      <!-- react-text: 78 -->,
      <!-- /react-text -->
      <!-- react-text: 79 -->
      <!-- /react-text --><span class="">-89</span></div><span class="text-muted">567222.67</span><br><span class="text-muted">7 January 2016, 9:29 pm</span><br></div>
  <div class="DebitsAndCreditsItem">
    <div class="DebitsAndCreditsItem-name">
      <!-- react-text: 87 -->NS Railways
      <!-- /react-text -->
      <!-- react-text: 88 -->,
      <!-- /react-text -->
      <!-- react-text: 89 -->
      <!-- /react-text --><span class="">-12.2</span></div><span class="text-muted">89357483.76</span><br><span class="text-muted">7 January 2016, 1:45 pm</span><br></div>
</div>

值也可以是double,string,char和boolean类型,或者是前一个变量(例如,a = 9, b=5 , c = 15 )。我试图安排以下正则表达式

a=b

但迄今未取得任何成功。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

您可以使用此正则表达式([a-zA-Z0-9]+\s*=\s*[a-zA-Z0-9]+)

regex demo

匹配

a = 9 
b=5
c = 15
a=b
b= true
s = false

修改

如果你想匹配这样的列表,例如:

a = 9 , b=5 , c = 15 , a=b , b= true , s = false

然后您可以使用像^([a-zA-Z0-9]+\s*=\s*[a-zA-Z0-9]+\s*,?\s*)*

这样的正则表达式

regex demo 2

在jave中你可以使用:

boolean m = "a = 9 , b=5 , c = 15 , a=b , b= true , s = false".
                matches("^([a-zA-Z0-9]+\\s*=\\s*[a-zA-Z0-9]+\\s*,?\\s*)*");//true

答案 1 :(得分:1)

试试这个:

解决方案1 ​​

(\s*[A-Za-z0-9]\s*=\s*\S*(,)?)+

如果要捕获列表中的所有元素,请:

解决方案2

 ((\s*[A-Za-z0-9]\s*=\s*\S*(,)?)+)
除了一个小的变化外,

YCF_L解决方案看起来也不错:

^([a-zA-Z0-9] + \ s * = \ s * [a-zA-Z0-9] + \ s *,?\ s *)* < / p>

第二部分忽略了你可以拥有双倍值的事实。 它仍然会匹配,但是如果要捕获上面的解决方案2 中的值,但使用YCF_L解决方案,那么小数部分将被删除。

以下是捕捉重复群组的绝佳链接:

http://www.regular-expressions.info/captureall.html