正则表达式 - 从给定字符串中提取子字符串

Vin*_*ent 9 regex xpath substring

我这里有一个字符串This is a string: AAA123456789.

所以这里的想法是AAA123456789使用正则表达式提取字符串.

我将它与X-Path结合在一起.

注意:如果有帖子,请引导我.

我认为,正确的,我应该substring(myNode, [^AAA\d+{9}]),

我不太确定正则表达式部分.

我们的想法是在遇到"AAA"时提取字符串,只有数字,但只有9个后续数字.

Dim*_*hev 9

纯XPath解决方案:

substring-after('This is a string: AAA123456789', ': ')
Run Code Online (Sandbox Code Playgroud)

产生:

AAA123456789
Run Code Online (Sandbox Code Playgroud)

XPath 2.0解决方案:

tokenize('This is a string: AAA123456789 but not an double',
              ' '
              )[starts-with(., 'AAA')]
Run Code Online (Sandbox Code Playgroud)

要么:

tokenize('This is a string: AAA123456789 but not an double',
              ' '
              )[matches(., 'AAA\d+')]
Run Code Online (Sandbox Code Playgroud)

要么:

replace('This is a string: AAA123456789 but not an double',
              '^.*(A+\d+).*$',
              '$1'
              )
Run Code Online (Sandbox Code Playgroud)

  • @Vincent,这意味着:用(第一对)括号内的子表达式替换整个字符串(如果它包含形式为 `A+\d+` 的子字符串)。`replace` 的第三个参数必须包含一个字符串,指定替换每个目标的内容。它允许按数字(位置)“捕获引用”。在此处阅读有关 `replace()` 的更多信息:http://www.w3.org/TR/xpath-functions/#func-replace (2认同)
  • 先生,有没有您尚未回答或不知道答案的 XPath 问题?:-) (2认同)