Split Regular Expression Function

I am new to this site and new to Python.

So, I learn about regular expressions, and I worked with Google examples here .

I did one of the "Search" examples, but I changed the "Search" to "Split" and slightly changed the search template to play with it, here is the line

print re.split(r'i', 'piiig')

(note that in the text "piiig" there is 3 'i)

The output has only 2 spaces in which it was split.

['p', '', '', 'gs']

Just wondering why this gives this result. This is not a problem in real life and has nothing to do with it, but I think I can handle it later and want to know what is going on.

Does anyone know what is happening ???

+3
source share
3

, i ,:

print re.split(r',', 'p,,,g')

, , a 'p', a 'g' '' .

+6

split . - i s.

join i , .

piiig, p- i - i - i -g ( )

+2

... ( Java, python)

String       Text     = "piiig";
List<String> Spliteds = new ArrayList<String>();
String       Match    = "";
int  I;
char c;
for (I = 0; I < Text.length; I++) {
    c = Text.charAt(I);
    if (c == 'i') {
        Spliteds.add(Match);
        Match = "";
    } else {
        Match += c;
    }
}
if (Match.length != 0)
    Spliteds.add(Match);

, ...

 At the end of each loop:
When: (I == 0) => c = 'p'; Match = "p"; Spliteds = {};
When: (I == 1) => c = 'i'; Match =  ""; Spliteds = {"p"};
When: (I == 2) => c = 'i'; Match =  ""; Spliteds = {"p", ""};
When: (I == 3) => c = 'i'; Match =  ""; Spliteds = {"p", "", ""};
When: (I == 4) => c = 'g'; Match = "g"; Spliteds = {"p", "", ""};
At the end of the program:
      (I == 4) => c = 'g'; Match = "g"; Spliteds = {"p", "", "", "g"};

RegEx "i", "i" "i".

, .

0

Source: https://habr.com/ru/post/1760233/


All Articles