NOTE: This page has been translated automatically from Russian to English. Original page.



Regular expressions without external components? Easily!

The hard way to organize a line check using the regular expression in 1C. In this case, no external components are not damaged. And on the platform (Linux, MustDie) - is independent.

Hello comrades!

In order not to delay, once the code. Who wants to understand what's actually the case, let them read articles on XDTO. Regular expressions describe below, but articles on the subject and so the mass.

Code:

 & NaKliente
ProveritStroku Function (string, facet)
	Reading = New ChtenieXML;
	Chtenie.UstanovitStroku (
				"<Model xmlns =" ​​"http://v8.1c.ru/8.1/xdto" "xmlns: xs =" "http://www.w3.org/2001/XMLSchema" "xmlns: xsi =" "http : //www.w3.org/2001/XMLSchema-instance "" xsi: type = "" Model "">
				| <Package targetNamespace = "" sample-my-package "">
				| <ValueType name = "" testtypes "" base = "" xs: string "">
				| <Pattern> "+ Facet +" </ pattern>
				| </ ValueType>
				| <ObjectType name = "" TestObj "">
				| <Property xmlns: d4p1 = "" sample-my-package "" name = "" TestItem "" type = "" d4p1: testtypes "" />
				| </ ObjectType>
				| </ Package>
				| </ Model> ");

	Model = FabrikaXDTO.ProchitatXML (Reading);
	MoyaFabrikaXDTO FabrikaXDTO = new (model);
	Package = MoyaFabrikaXDTO.Pakety.Poluchit ( "sample-my-package");
	Test = MoyaFabrikaXDTO.Sozdat (Paket.Poluchit ( "TestObj"));

	Attempt
		Test.TestItem = string;
		Return Truth
	An exception
		Return False
	KonetsPopytki;
	
KonetsFunktsii
	
& NaKliente
Model Procedure (Team)
	
	Report (ProveritStroku ( "01.01.2012", "\ d {2} \ \ d {2} \ \ d {4}.."));
	Report (ProveritStroku ( "01.01.20121", "\ d {2} \ \ d {2} \ \ d {4}.."));
	
KonetsProtsedury

All.

For those who are a little ...

So, that way years ago endtsat programmers decided to simplify the search, replace and check for sootvetvie different lines, as them, I think, tired of every time to write something like:

 If the medium (strData, 1.1) < "0" or media (strData, 1,1)> "9" Then Error = True;
ENDIF;

If the medium (strData, 2.1) < "0" or media (strData, 2,1)> "9" Then Error = True;
ENDIF;

If the medium (strData, 3.1) <> "." Then Error = True;
ENDIF;

// ... 

As a result, in all normal programming languages ​​have been implemented library containing functions and procedures for working with regular expressions, and the life of developers qualitatively improved, for the monstrous code could be replaced by a much more simple:

 Error = NOT ProveritStroku (StrData, "\ d {2} \ \ d {2} \ \ d {4}.."); 

Yes, developers became literate oh how simple. But what about the rest? Correctament answer, of course - to study, study and study again! )))

So, the easiest thing you need to master the test patterns:

. - Any character

+ - One or more times, an example of "+." - One or more symbol.

* - Zero or more times, an example of ". *" - Any number of any characters (even one).

[Nm] - symbol of m and n, example: "[0-9] +" - one or more digits (a).

\ D - a figure, an example \ d + - one or more digits (a).

\ D - does not figure.

\ S - whitespace - TAB, space, newline, carriage return, etc.

\ S - non-whitespace character.

\ W - letters, numbers, underscore.

\ W - not a letter, not a digit or underline, respectively.

^ - The beginning of the text, such as "^ \ d +" - line begins with a number.

$ - End of the text, such as "\ D + $" - the string ends with a digit.

{M, n} - pattern for from m to n characters, such as "\ d {2,4}" - from two to four digits. You can specify one and only figure to strict sootvetviya.

\ - Escapes special characters. For example, "\." - A dot.

1C:Enterprise Developer's Community