-1

I want to use jmeters regular expression extractor to catch a link from an HTTP response I have. How do I catch only whats inside the <a href="TEXT"></a>? I want the TEXT.

<a([^>]+)>(.+?)<\/a>

The expression above gives me the whole link with the a tag and href.

afsantos
  • 5,178
  • 4
  • 30
  • 54
misschoksondik
  • 561
  • 2
  • 6
  • 19

3 Answers3

3

I would rather recommend not using regular expressions for getting data from HTML as href attribute may be located in differently, at new line, etc. See the epic comment on SO for detailed explanation.

JMeter provides 2 test elements which can be used to extract href attribute from HTML page links:

XPath Example

  1. Add XPath Extractor as a child of the request (just like Regular Expression Extractor)
  2. Configure it as follows:
    • If your response is not XHTML compliant - check Use Tidy box
    • Reference name - anything meaningful, i.e. href
    • XPath query - //a/@href
  3. You can refer to extracted link URL as ${href} anywhere in current thread group.
  4. In case of multiple matches URLs can be accessed as ${href_1} ${href_2} etc.

For more information on the XPath Extractor see Using the XPath Extractor in JMeter guide

CSS/JQuery Example

  1. Add CSS/JQuery Extractor as a child of the request
  2. Configure it as follows:
    • Reference name - any variable name, i.e. href
    • CSS/JQuery expression - a
    • Attribute - href
    • Match no:
      • default is blank - will return the first link
      • any number > 0 - will return match number
      • 0 - will return random link URL
      • -1 - will return all link URLs and store them as ${href_1} ${href_2} etc.

For CSS/JQuery expressions building information refer to JSOUP selector syntax guide

Community
  • 1
  • 1
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

Try with this:

<a[^>]* href="([^"]*)"

regular expression for finding 'href' value of a <a> link

Community
  • 1
  • 1
Andrés Oviedo
  • 1,388
  • 1
  • 13
  • 28
0

Try this.

use group 1 to get the content from tag.

<a(?: [^>]+)?>((?:(?!<\/?a[ >]).)*)<\/a>

SEE DEMO: http://regex101.com/r/rV3eH6/1

depsai
  • 405
  • 2
  • 14