How to set up regular expressions?

Antrax - Сonsultation on system performance

How to set up regular expressions?

Examples from ANTRAX:
1. Parse the response string of the balance check using the regular expression. In this case, you need to get the expression '2.00'
Full post:
Na Vashem schete 2.00 grn. Tarif 'Vodafone Light'. Nomer deystvitelen do 07.01.2019. Upravlyaite nomerom v My Vodafone App - www.vodafone.ua

In order to compare a certain value, you must first capture the entire message and then use the regular expression to specify what it is necessary to compare.

To do this, you must select a word or a phrase before the value or after, you can also specify both that there would be a more accurate regular expression that you need to compare, precede it with ". *", Given the "spaces" before the value that needs to be checked, to specify a numeric value, we use \ d to do this, in order to take a few numbers it is necessary after specifying \ d + if the value is "." or ",", then it is necessary to escape them with the "\" symbol, that is, it will look like this "\." or "\,", then we specify the values ​​that follow, it can be characters that can be taken with ". *" or numbers that can be taken with "\ d +", but after the characters "." or "," it is desirable to specify the exact value of the numbers, you can use "\ d" with the additional values ​​"{}" - this setting is responsible for the number of characters you need to take for the regular expression, respectively, since from 2.00, specify that after " . " goes 2 numbers it is necessary in parentheses "{}" to specify 2, that is \ d {2}. Also, if you need to make a comparison, the value that you need to compare should be taken in parentheses "()", these regular expressions will look like this (\ d +) and (\ d {2}). It also happens sometimes that the message has a line break that also needs to be specified with "(? S)". And then our regular expression for comparison 2.00 will look like this:

.*Na Vashem schete (\d+)\.(\d{2}) grn.*(?s).*

Consider this regular expression:

The value ". *" Is responsible for selecting all the characters in this line

"Na Vashem schete" is a phrase that is before the value 2.00, that is, we made a binding to the value 2.00, indicating the word before it

(\ d +) is a regular expression for the value 2 from the number 2.00

\. is a regular expression for a value. from the date 2.00

(\ d {2}) is a regular expression for the value 00 from the number 2.00

"grn" is a phrase that is after the value 2.00, that is, we made a binding to the value 2.00, indicating the word after it

. * (? s). * is a regular expression. which is responsible for the possible transfer of a line, is necessary for the fact that if the message is with line wrapping

Sometimes messages are different with or without a transfer and in order to avoid creating two regular expressions, the possible transfer of a line is indicated.

2. The parsing of the answer line of the balance check when Hebrew is received.

Full post:

ستاسو فعلی حساب0.00 افغانی اوتر07/07/2018 اعتبارلری 

Regular expression:

.*(\d+)\.(\d{2})*(?s).*

3. Parse the string for auto-replacement sms, if you need to replace one expression. In this example, you need to replace the 'confirmation code:'

In order to replace the text with SMS, it is also necessary to select the whole message and then split it into parts, those that do not need to be changed, you can specify them as ". *" And those that need to be replaced, you need to specify a word or a phrase

Complete message:

Enter your personal code: 558877

Regular expression:

(.*)(code)(.*)

Where:

The regular expression (. *) is responsible for the phrase "Enter your personal" - the first part of the message

(confirmation code) is a regular expression that is responsible for the exact phrase " code"

(. *) is a regular expression that is responsible for the phrase ": 558877"

That is, we divided the text of the message into 3 parts:

1 part to the words "code"

2 part of the word "code"

Part 3 after the words "code"

Each part of the expression acts as a "part" - where the "part" is the ordinal number of the pair "()", that is, the first open "(" (bracket), will be as $ 1, the next "(" (bracket) as $ 2

In the Replace by field, you must specify:

$1$3

Results:

Enter your personal: 558877
4. Parsing the line for auto-replace sms, if you need to replace multiple expressions. In this case, you need to replace G-, google, code.
Full post:
G-679425 is your google verification code
Regular expression:
(G\-)(.*)(google)(.*)(code)

In filed Replace by need write:

$2$4CoDe
Result of replacement:
679425 is your verification КoДe

where:

. * matches any character (except row terminators);

(\ d) - match with a digit (equal to [0-9]);

(\ d +) - match with any number of digits;

(\ d {2}) - matches exactly 2 times;

\. - Shielding the character;

(? s) - wrapping the line.



 


Loading...