4

我是的新手;我希望我能很好地向你描述我的问题。

我正在尝试使用正则表达式从 xml 元素中提取 ItemID 属性。然后我在另一个请求中使用它。这是我试图从中提取 ItemID 的 XML 响应:

<?xml version="1.0" encoding="UTF-8"?>
<Promise >
 <SuggestedOption>
      <Option TotalShipments="1">
           <PromiseLines TotalNumberOfRecords="1">
                <PromiseLine ItemID="Col_001" >
                     <Assignments>
                          <Assignment InteractionNo="1" >
                          </Assignment>
                     </Assignments>
                </PromiseLine>
           </PromiseLines>
     </Option>
 </SuggestedOption>
</Promise>

我的正则表达式提取器设置如下:

Reference Name: item
Regular Expression: .?ItemID=(.+?)*
Template: $1$
Match No.: 1

在第二个请求中,我将 ItemID 设置如下 ... ItemID=${item} ...

我知道当我使用设置为“Col_001”的默认值时,它工作正常。所以我的表达显然有问题。

4

3 回答 3

4

试试这个表达式:

\bItemID\s*=\s*"([^"]*)"

解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  ItemID                   'ItemID'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  =                        '='
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  "                        '"'
于 2012-11-05T00:48:09.747 回答
1

另一种选择是使用 xpath 提取器来执行此操作:

于 2012-11-05T06:53:39.650 回答
1

尝试以下正则表达式:

ItemID="(.+)"
于 2014-11-27T13:13:44.763 回答