Difficulty with Regex

Asked

Viewed 50 times

0

I have the following content in a string:

|1300|11349|03042016|10857,000|0,000|10857,000|444,470|10412,530|25,530|0,000|10387,000| |1310|8|4657,000|0,000|4657,000|444,470|4212,530|5,530|0,000|4207,000| |1320|25||||||281647,640|281203,170|0,000|444,470| |1320|26|||||||308202,420|308202,420|0,000|0,000| |1310|11|6200,000|0,000|6200,000|0,000|6200,000|20,000|0,000|6180,000| |1320|28||||||439837,300|439837,300|0,000|0,000| |1300|11518|03042016|8057,000|0,000|8057,000|560,519|7496,481|0,000|25,519|7522,000| |1310|7|8057,000|0,000|8057,000|560,519|7496,481|0,000|25,519|7522,000| |1320|13|||||||420207,150|420146,581|0,000|60,569| |1320|14||||||457108,130|456806,946|0,000|301,184| |1320|21||||||460614,020|460518,267|0,000|95,753| |1320|22||||||544902,750|544799,677|0,000|103,073|

I’d like to break that string into one array for the blocks that start with |1300|

I’d take a vector of two positions, thus forming each position of the vector for example:

Position 1:

|1300|11349|03042016|10857,000|0,000|10857,000|444,470|10412,530|25,530|0,000|10387,000| |1310|8|4657,000|0,000|4657,000|444,470|4212,530|5,530|0,000|4207,000| |1320|25||||||281647,640|281203,170|0,000|444,470| |1320|26|||||||308202,420|308202,420|0,000|0,000| |1310|11|6200,000|0,000|6200,000|0,000|6200,000|20,000|0,000|6180,000| |1320|28||||||439837,300|439837,300|0,000|0,000|

Position 2

|1300|11518|03042016|8057,000|0,000|8057,000|560,519|7496,481|0,000|25,519|7522,000| |1310|7|8057,000|0,000|8057,000|560,519|7496,481|0,000|25,519|7522,000| |1320|13|||||||420207,150|420146,581|0,000|60,569| |1320|14||||||457108,130|456806,946|0,000|301,184| |1320|21||||||460614,020|460518,267|0,000|95,753| |1320|22||||||544902,750|544799,677|0,000|103,073|

I tried the code below but it didn’t work

string[] arr1300 = Regex.Split(conteudo, @"(\|1300\|)");
  • 1
  • 1

    @Gypsy omorrisonmendez knew no. I took a look at the package. seems to have enough. thank you!

1 answer

3


I believe that a Split normal solves your problem:

string stringSplit = "|1300|";
string[] arr1300 = conteudo.Split(new string[] { stringSplit }, StringSplitOptions.RemoveEmptyEntries);

After the Split just concatenate the "|1300|" so that the positions stay the same you want:

string posicao1 = stringSplit + arr1300[0];
string posicao2 = stringSplit + arr1300[1];

If you have more than two positions you can concatenate the "|1300|" in all positions at once:

arr1300 = arr1300.Select(x => stringSplit + x).ToArray();
  • I think he asked using regex.

  • I’m gonna try. Actually I had tried with Split, but |1300| was removed, and I didn’t want to do a for put it again; I think the last code suggestion will solve.

  • It worked, although the split removed a part of the content, the last instruction that makes Select concatenating the delimiter met perfectly and with few lines.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.