Crmgo Blog

Using Regular Expressions in 1C

Using Regular Expressions in 1C

Occasionally, there is a need to check whether a string matches a specific pattern, find or replace substrings that match the pattern. Regular expressions are used for these purposes. In 1C, this technology is available through the COM object VBScript.RegExp. This object is built into the Windows operating system and…

Using Regular Expressions in 1C
Occasionally, there is a need to check if a string matches a specific pattern, or to find or replace substrings that match a pattern. Regular expressions are used for these purposes. In 1C, this technology is available via the COM object VBScript.RegExp. This object is built into the Windows operating system and is ready to use.

1. Creating the object.

RegExp = Новый COMОбъект("VBScript.RegExp");

2. Properties.

Property Data Type Description Default Value Example
Global Boolean Check the string up to the first pattern match (False) or throughout the entire text (True) False

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "abc";
ИсходнаяСтрока = "abc 123 abc";
Результат = RegExp.Replace(ИсходнаяСтрока, "def"); // def 123 abc
RegExp.Global = Истина;
Результат = RegExp.Replace(ИсходнаяСтрока, "def"); // def 123 def
IgnoreCase Boolean Case-sensitive (False) or case-insensitive (True) False

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "АБВ";
ИсходнаяСтрока = "абв 123";
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // абв 123
RegExp.IgnoreCase = Истина;
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // где 123
MultiLine Boolean Multi-line (True) or single-line (False) mode False

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
RegExp.Pattern = "^абв";
ИсходнаяСтрока = "абв 123 абв" + Символы.ПС + "абв";
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // где 123 абв
// абв
RegExp.Multiline = Истина;
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // где 123 абв
// где
Pattern String The pattern used – a text sample consisting of ordinary characters and/or special metacharacters

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "абв";
ИсходнаяСтрока = "абв 123";
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // где 123

3. Methods.

Method Description Return Value Example
Replace(String, Substring) Replaces occurrences matching the pattern (Pattern property) in the original string with the specified substring Modified string

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "абв";
ИсходнаяСтрока = "абв 123";
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // где 123
Test(String) Checks if the string matches the pattern Boolean

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "абв+";
ИсходнаяСтрока = "абввввггг";
Результат = RegExp.Test(ИсходнаяСтрока); // Истина
ИсходнаяСтрока = "аввввггг";
Результат = RegExp.Test(ИсходнаяСтрока); // Ложь
Execute(String) Finds occurrences in the original string that match the pattern Collection of found substrings as a composite object

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "абв+";
RegExp.Global = Истина;
ИсходнаяСтрока = "абв 123 абв абвв 456";
Matches = RegExp.Execute(ИсходнаяСтрока);
Для Счетчик = 0 По Matches.Count - 1 Цикл 
    Match = Matches.Item(Счетчик);
	// Найденное значение (подстрока) + Индекс первого символа найденной подстроки в строке-оригинале + Длина найденной подстроки
	Результат = Match.Value + ", " + Match.FirstIndex + ", " + Match.Length; // абв, 0, 3
// абв, 8, 3
// абвв, 12, 4
КонецЦикла;

4. Metacharacters.

Characters Description Example
\ Indicates that the next character is a special character. The sequence "\\" corresponds to "\", and the sequence "\(" corresponds to "(".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "^[A-Z]:\\$";
ИсходнаяСтрока = "C:\";
Результат = RegExp.Test(ИсходнаяСтрока); // Истина
^ Defines the beginning of the input string. If the Multiline property is set, it also defines the position immediately after "\n" (newline) and "\r" (carriage return).

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
RegExp.Pattern = "^абв";
ИсходнаяСтрока = "абв 123 абв" + Символы.ПС + "абв";
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // где 123 абв
// абв
RegExp.Multiline = Истина;
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // где 123 абв
// где
$ Defines the end of the input string. If the Multiline property is set, it also defines the position immediately before "\n" (newline) and "\r" (carriage return).

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
RegExp.Pattern = "абв$";
ИсходнаяСтрока = "абв 123 абв" + Символы.ПС + "абв";
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // абв 123 абв
// где
RegExp.Multiline = Истина;
Результат = RegExp.Replace(ИсходнаяСтрока, "где"); // абв 123 где
// где
* Defines zero or more characters preceding it. Equivalent to {0,}.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "длинноше*";
ИсходнаяСтрока = "длинношеее";
Результат = RegExp.Test(ИсходнаяСтрока); // Истина
+ Defines one or more characters preceding it. Equivalent to {1,}.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Pattern = "длинноше+";
ИсходнаяСтрока = "длинношеее";
Результат = RegExp.Test(ИсходнаяСтрока); // Истина
? Defines zero or one character preceding it. Equivalent to {0,1}. If this metacharacter immediately follows constructs (*, +, ?, {n}, {n,}, {n,m}), it modifies the search algorithm as illustrated in the example below.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "000111000";
RegExp.Pattern = "0+";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
RegExp.Pattern = "0+?";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 6
{n} Defines an exact number of characters preceding it. "n" is a non-negative integer.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "000111001110000";
RegExp.Pattern = "0{3}";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
{n,} Defines a minimum number of characters preceding it. "n" is a non-negative integer. "{0,}" is equivalent to "*", and "{1,}" is equivalent to "+".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "000111001110000";
RegExp.Pattern = "0{3,}";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
{n,m} Defines the number of characters preceding it (from n to m). "n" and "m" are non-negative integers, with n <= m. "{0,1}" is equivalent to "?".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "00011101110000";
RegExp.Pattern = "0{2,3}";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
. Defines any character except "\n" (newline). To define any character including "\n", use "[\s\S]".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "00" + Символы.ПС + "00";
RegExp.Pattern = ".";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 4
RegExp.Pattern = "[\s\S]";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 5
(pattern) Captures the matched pattern into the SubMatches collection. To match the characters "(" and ")", use "\(" and "\)".

RegExp = Новый COMОбъект("VBScript.RegExp");
ИсходнаяСтрока = "One = 111" + Символы.ВК + "Two = 222" + Символы.ВК + "Three = 333";
// прочитаем параметр "Two"
RegExp.Pattern = "Two = (.+\r)";
Matches = RegExp.Execute(ИсходнаяСтрока);
Match = Matches.Item(0);
Результат = Match.Value; // Two = 222
Submatches = Match.Submatches;
Для Счетчик = 0 По Submatches.Count - 1 Цикл
	Результат = Submatches.Item(Счетчик); // 222
КонецЦикла;
(?:pattern) Useful for combining parts of a pattern using the "|" ("or") character. For example, "госпо(?:дин|жа)" is shorter than "господин|госпожа". Does NOT capture the match into the SubMatches collection.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "господин госпожа господа";
RegExp.Pattern = "госпо(?:дин|жа)";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
(?=pattern) "Windows(?=95|98)" matches the substring "Windows" in the string "Windows95", but not in "WindowsNT". The next match starts immediately, not after the characters within the parentheses. Does NOT capture the match into the SubMatches collection.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "WindowsWindowsWindows";
RegExp.Pattern = "Windows(?=Win)";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
Для Счетчик = 0 По Matches.Count - 1 Цикл
	Результат = Matches.Item(Счетчик).Value; // Windows
// Windows
КонецЦикла;
(?!pattern) "Windows(?!95|98)" matches the substring "Windows" in the string "WindowsNT", but not in "Windows95". Otherwise, it is fully analogous to "(?=pattern)".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "WindowsWindowsWindows";
RegExp.Pattern = "Windows(?!Win)";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
Для Счетчик = 0 По Matches.Count - 1 Цикл
	Результат = Matches.Item(Счетчик).Value; // Windows
КонецЦикла;
x|y Logical "or" condition. "вз|перевод" matches "вз" and "перевод". "(вз|пере)вод" matches "взвод" and "перевод".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "Windows Lindows Linux";
RegExp.Pattern = "(Wi|Li)ndows";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
[abc] Character set. Matches any of the specified characters.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "Windows Lindows Linux";
RegExp.Pattern = "[WL]indows";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
[^abc] Negated character set. Matches any character not specified.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "Windows Lindows Linux";
RegExp.Pattern = "in[^uv]";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
[a-z] Character range. Matches any character within the range.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "Windows Lindows Linux";
RegExp.Pattern = "[L-W]indows";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
[^a-z] Negated character range. Matches any character that is not in the range.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "Windows Lindows Linux";
RegExp.Pattern = "[^A-K]indows";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
\b \B \b - Word boundary, i.e., the position between the end of a word and a space. \B - Non-word boundary.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "Windows Windows Windows98";
RegExp.Pattern = "ows\b";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
RegExp.Pattern = "ows\B";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\d \D \d - Digit. Equivalent to [0-9]. \D - Non-digit. Equivalent to [^0-9].

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "WindowsNT Windows2000 Windows98";
RegExp.Pattern = "Windows\d";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
RegExp.Pattern = "Windows\D";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\w \W \w - Any word character, including the underscore. Equivalent to [A-Za-z0-9_]. \W - Opposite of "\w". Equivalent to [^A-Za-z0-9_].

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "Windows® Windows2000 Windows98";
RegExp.Pattern = "Windows\w";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
RegExp.Pattern = "Windows\W";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\s \S \s - Whitespace character. Equivalent to [\f\n\r\t\v]. \S - Non-whitespace character. Equivalent to [^\f\n\r\t\v].

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "Windows Windows Windows98";
RegExp.Pattern = "Windows\s";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
RegExp.Pattern = "Windows\S";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\cx Control character. The parameter "x" must be in the range A-Z or a-z. For example, "\cM" represents Control-M or carriage return, "\cJ" represents a newline, "\cL" represents a form feed, "\cI" represents a tab, and "\cK" represents a vertical tab.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = Символы.ВК + Символы.ВК;
RegExp.Pattern = "\cM";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 2
\f Form feed character. Equivalent to "\x0c" and "\cL".
\n Newline character. Equivalent to "\x0a" and "\cJ".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = Символы.ПС;
RegExp.Pattern = "\n";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\r Carriage return character. Equivalent to "\x0d" and "\cM".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = Символы.ВК;
RegExp.Pattern = "\r";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\t Tab character. Equivalent to "\x09" and "\cI".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = Символы.Таб;
RegExp.Pattern = "\t";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\v Vertical tab character. Equivalent to "\x0b" and "\cK".

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = Символы.ВТаб;
RegExp.Pattern = "\v";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\xn Hexadecimal ASCII code of a character. Must contain exactly two characters.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = Символы.Таб;
RegExp.Pattern = "\x09";
Matches = RegExp.Execute(ИсходнаяСтрока);
Результат = Matches.Count; // 1
\num Backreference to previously captured (matched) substrings (SubMatches). "num" is a positive integer representing the index of the match in the SubMatches collection; indexing starts at 1. For example, "(.)\1" matches two identical characters occurring consecutively.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "0:0:1-2:3:3-2:2:1-4:5:5-7:7:8";
// найдём группы, в которых две последние цифры совпадают
RegExp.Pattern = "(\d)(:)(\d)(:)\3";
Matches = RegExp.Execute(ИсходнаяСтрока);
Для Счетчик = 0 По Matches.Count - 1 Цикл
    Match = Matches.Item(Счетчик);
	Результат = Match.Value; // 2:3:3
// 4:5:5
КонецЦикла;
\un Defines a Unicode character by its hexadecimal code. "n" is a four-digit hexadecimal code.

RegExp = Новый COMОбъект("VBScript.RegExp");
RegExp.Global = Истина;
ИсходнаяСтрока = "© Microsoft © Sun";
RegExp.Pattern = "\u00A9 Sun";
Matches = RegExp.Execute(ИсходнаяСтрока);
Для Счетчик = 0 По Matches.Count - 1 Цикл
    Match = Matches.Item(Счетчик);
	Результат = Match.Value; // © Sun
КонецЦикла;

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.

Categories: 1C