Preface when I was learning Python, I found that the sharding Technology in Python is very interesting. I am also a fan of regular expressions. I usually use C # for many times, therefore, we decided to implement the sharding Technology in Python in C # and add it to the personal class library, so that we can lick the Python taste when writing C # code in the future. Here, we will briefly explain the sharding Technology in Python. Other Python Predecessors will also explain this technology in a variety of ways. Here, we will briefly explain it so that readers can know what is going on, if you want to learn more about the Python sharding technology, it is not suitable for you here. The sharding object can be a string or a sequence. This article provides the implementation method of the string. In fact, the sequence is similar. I will also provide the implementation code of the string and the sequence at the end, with a unit test. Well, I don't want to talk much about it. Actually, after reading the above Python version results, I 'd like to know a little bit about it. The part template is: Variable [start position: end position: Step Size]. For example: a = "abcdfeghij ", then the result of a [] is the start of zero position (the left side of a), step 1 (that is, continuous fetch), and end at 2 (starting from the left side of: 0-> (left of a), 1-> (left of B, right of a), 2-> (left of c, right of B)-> end ), okay, the result is "AB ". Note: 1. The step size can be ignored and not written (the default value is 1). If the step size is 2, skip 1 for each one and so on. 2. You can use the negative number a [-3:-]. The result is "ef". Readers can push it by themselves, which is very simple. 3. The sharding technique can be applied to strings or sequences. The implementation process requires the regular expression technology, which may be difficult for some friends, but I will try my best to explain it a little simpler. First, you need to use parts. There are three parameters to control: Start position, end position, and step size. Therefore, the regular expression should embed these three parameter variables and dynamically generate a regular expression during matching. The following shows where the three parameters need to be put: Start position: 1. background here, we use a forward-directed view. What is a forward-directed view is a backward view of the matched position. The forward-looking content must conform to the context content. For example: Text: abc, regular Expression :(? <= A) B. It must be backward-ordered :(? <= Exp), this regular expression means to find B and then look back (left). If it is a, the match is successful, the result is B (because reverse view is not included in the result ). 2. Implement [\ s \ S] to represent an arbitrary symbol. Place the part containing the location parameter on the left side to form such a regular expression :(? <= ^ [\ S \ S] {StarIndex,}) [\ s \ S] note that StarIndex is a variable, which can be 0, 1, 2, 3 ..! Taking StarIndex = 2 (in fact, the position is 2) as an example, match an arbitrary character, and look back (on the left) to start with-> 2 or multiple arbitrary characters. End position: 1. background here we use the affirmative sequence loop view. What is the affirmative sequence loop view? It is the point where the match is located that looks forward. The forward content must conform to the loop View content. For example: Text abc, regular Expression :(? = B) a. It must be a reverse-order view Symbol :(? = Exp), this regular expression means finding a and then looking forward (right side). If B is used, the matching is successful and the result is a (because sequential view is not included in the result ). 2. The implementation has the basis of the previous expression. I need to add a sequence View to the end to form such an expression (after merging ):(? <= ^ [\ S \ S] {StarIndex,}) [\ s \ S] (? = [\ S \ S] {EndCount}). Note that EndCount is not the end position. formula: EndCount = String. Length-EndIndex. String Length-end position. Step Size: Finally, the step size is reached. This is a key step for connecting the above two parts. In fact, the basics have been discussed earlier. After the step size logic is added, the final regular expression :(? <= ^ [\ S \ S] {StarIndex ,})(? <= ^ [\ S \ S] {MiddleCount}) [\ s \ S] (? = [\ S \ S] {EndCount}). The Step size parameter is the Step variable. Here, the MiddleCount is changed in the loop. MiddleCount = MiddleCount + Step is used for each loop; loop To the final merging to form a result string !!. The instance demonstrates the matching process and result. String: a = "abcdefghij"; a. Cut (, 1); number of cycles: 3-0 = 3. Loop 1: expression :(? <= ^ [\ S \ S] {0 ,})(? <= ^ [\ S \ S] {0}) [\ s \ S] (? = [\ S \ S] {7}) loop 2: expression (? <= ^ [\ S \ S] {0 ,})(? <= ^ [\ S \ S] {1}) [\ s \ S] (? = [\ S \ S] {7}) loop 3: expression (? <= ^ [\ S \ S] {0 ,})(? <= ^ [\ S \ S] {2}) [\ s \ S] (? = [\ S \ S] {7}) Final merging result: "abc ". Finally, paste the result in Python :. public static class StringExpander {// <summary> // string Slicing Technology in Python, [start index: End index: step Size] /// </summary> /// <param name = "Str"> Target string </param> /// <param name = "StarIndex"> Start Index </param> /// <param name = "EndIndex"> end index </param> /// <param name = "Step"> Step value </param> // /<returns> </returns> public static String StringCut (this String Str, int32 StarIndex, Int32 EndIndex, Int32 Step = 1) {if (En DIndex <0) {EndIndex = Str. length + EndIndex;} if (StarIndex <0) {StarIndex = Str. length + StarIndex;} StringBuilder sb = new StringBuilder (); Int32 LoopTime = EndIndex-StarIndex; if (EndIndex> Str. length) {EndIndex = Str. length;} Int32 EndCount = Str. length-EndIndex; int j = StarIndex; for (int I = 0; I <LoopTime; I = I + Step) {String RegexString = @"(? <= ^ [\ S \ S] {"+ StarIndex + @",})(? <= ^ [\ S \ S] {"+ j + @"}) [\ s \ S] (? = [\ S \ S] {"+ EndCount + @"}) "; try {sb. append (Regex. match (Str, RegexString ). value);} catch {} j = j + Step;} return sb. toString () ;}/// <summary> // string Slicing Technology in Python, only the location // </summary> /// <param name = "Str"> Target string </param> /// <param name = "StarIndex"> location </param> // <returns> </returns> public static String StringCut (this String Str, int32 StarIndex) {if (StarIndex <0) {StarIndex = Str. length + StarIndex;} return Str. substring (StarIndex, 1 );}}