Skip to main content

Using Regular Expressions

General Information

In A-Parser, Perl/JavaScript-compatible regular expressions are used, which can be applied:

Detailed documentation on regular expressions can be found in the following sources:

In A-Parser, it is possible to process any result using a regular expression, for this the option Use regex is used:

Option to Parse custom result

Usage Specifics and Flags

  • Regular expressions are written without delimiters //
  • The following flags are supported:
    • i - case-insensitive search
    • s - dot matches all characters, including line breaks
    • g - global search or replacement

Additionally, it is possible to specify a flag within the regex itself, for example, searching for the word test in each line of the entire text (or page code, depending on what the regex is applied to) using the m flag (multi line - symbols ^ and $ work as the beginning and end of the line respectively):


Extraction of Arbitrary Information

Description of working with regular expressions in the task editor

Using the Use regex option or the Results Constructor, it is possible to use regular expressions to extract arbitrary information from parsing results, for example from the source HTML code of pages or from already prepared results

  • As Apply to, the result from the scraper is selected, which can be a simple result or an array
  • The regular expression is specified without delimiters, followed by the possibility to specify a flag
  • In Result Type, the type of result is indicated - Flat (simple result) or Array (array). If an array is selected as the source result or the g flag of the regular expression is used, the result will always be saved in an array. The name of the array is specified in the Name field
  • Each capturing bracket of the regular expression can be saved as a separate element, the name of the element is written in the corresponding field $1 to, $2 to... - where the number indicates the capturing bracket number
  • In the Regex field, you can use the template engine, which allows using the query as part of the regular expression

The newly created results can be used in formatting results, in the results builder, in filtering and deduplication of results, or in the next option Use RegEx. This option is similar to the results builder when using RegEx Match

Example of parsing links to images from the source HTML code

To solve this task, we use the scraper Net::HTTPNet::HTTP to obtain the source code of the page. We apply a regular expression with the isg flags to $data (the downloaded page), and save the results in the src elements of the images array. In the result format, we specify to output all src elements separated by a newline.

As a result of parsing for the request in the result file, we will get the following list:

img/[email protected]
Download example

How to import an example into A-Parser


Regular expression constructor

Starting with version 1.2.78, a Regular Expression Constructor was added.

It can be found in the Tools -> Regular Expression Constructor tab. You can also send the obtained page code directly to Test Parsing. For this, you need to enable debug mode and click on the link Go to RegEx Builder.

Open the page code in the regular expression constructor

In the constructor, there is an option to select the programming language in which the obtained regular expressions will be used.

To work with the constructor, you need to insert the source text into the field on the left (or it will be automatically inserted from Test Parsing when transitioning Go to Regex Builder). On the right, we configure the parameters of the future regular expression.

To compose a simple Regular Expression (for example, to get a title), it is enough to specify the necessary elements of the regular expression.

  • In the Before group field, we write the characters that are before the information we need
  • In the After group field, we write the characters that are after the required data
  • In the Group starts with field, we specify the characters with which the desired string should start
  • In the Group ends with field, we indicate the characters that should be at the end of the desired string
Example of getting a title using a regular expression constructor

As seen in the screenshot above, we are composing a regular expression that will select the site's title. We put <title> before the group and </title> after the group, and also, for example, indicate that the desired string starts with the letter W.

For full testing of the obtained regular expression, there is an option to enable the necessary flags: g, s, and i.

You can also compose more complex regular expressions, in which there are 2 or more groups. For example, let's try to compose a regular expression for collecting all links and anchors in the list <li>. For this, we need to enable the g flag and add another search group, as the first group will contain links, and the second anchors.

Example of using groups in the regular expression constructor

After setting the necessary parameters for both groups, we get the regular expression:

<li><a href="(.+?)">(.+?)<\/a

To check the regular expression, press the Test button: