Find multiple matches and place all at multiple location instance
Posted: 26 Nov 2022, 06:08
Hi all, so I have this comma delimited file that I need to import but I need to do some replacements first. A sample is as shown below;
item A,<u>1 a1</u> red<u>2 a2</u> etc,content,,,,,,,end
1 item A,content,content,,,,,,,end
2 item A,content,content,,,,,,,end
item B,<u>1 b1</u> yellow<u>2 b2</u> green<u>3 b3</u>etc,content,,,,,,,end
1 item B,content,content,,,,,,,end
2 item B,<u>1 2b1</u> black<u>2 2b2</u> etc,content,,,,,,,end
2 item B 1,content,content,,,,,,,end
2 item B 2,content,content,,,,,,,end
3 item B,content,content,,,,,,,end
Each main item (eg. item A) is followed by its subitems (eg. 1 item A), which might also have sub-subitem (eg. 2 item B 1) as shown above. The objective is to perform a multiple match on the main items and place all matches at a specific location in both the main item and its subitems as shown below;
item A,<u>1 a1</u> red<u>2 a2</u> etc,content,,,,1 a1<br>2 a2<br>3 a3,,,end
1 item A,content,content,,,,1 a1<br>2 a2<br>3 a3,,,end
2 item A,content,content,,,,1 a1<br>2 a2<br>3 a3,,,end
item B,<u>1 b1</u> yellow<u>2 b2</u> green<u>3 b3</u>etc,content,,,,1 b1<br>2 b2<br>3 b3,,,end
1 item B,content,content,,,,1 b1<br>2 b2<br>3 b3,,,end
2 item B,<u>1 2b1</u> black<u>2 2b2</u> etc,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
2 item B 1,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
2 item B 2,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
3 item B,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
The regular expression (?:^[^,]+,.*?<u>(\d{1,2} .+?)</u>[^,]*,) does match, but the challenge is how to get the multiple matches and place them at the desired location in all respective major and sub items. Can anyone help with this? Thanks already.
item A,<u>1 a1</u> red<u>2 a2</u> etc,content,,,,,,,end
1 item A,content,content,,,,,,,end
2 item A,content,content,,,,,,,end
item B,<u>1 b1</u> yellow<u>2 b2</u> green<u>3 b3</u>etc,content,,,,,,,end
1 item B,content,content,,,,,,,end
2 item B,<u>1 2b1</u> black<u>2 2b2</u> etc,content,,,,,,,end
2 item B 1,content,content,,,,,,,end
2 item B 2,content,content,,,,,,,end
3 item B,content,content,,,,,,,end
Each main item (eg. item A) is followed by its subitems (eg. 1 item A), which might also have sub-subitem (eg. 2 item B 1) as shown above. The objective is to perform a multiple match on the main items and place all matches at a specific location in both the main item and its subitems as shown below;
item A,<u>1 a1</u> red<u>2 a2</u> etc,content,,,,1 a1<br>2 a2<br>3 a3,,,end
1 item A,content,content,,,,1 a1<br>2 a2<br>3 a3,,,end
2 item A,content,content,,,,1 a1<br>2 a2<br>3 a3,,,end
item B,<u>1 b1</u> yellow<u>2 b2</u> green<u>3 b3</u>etc,content,,,,1 b1<br>2 b2<br>3 b3,,,end
1 item B,content,content,,,,1 b1<br>2 b2<br>3 b3,,,end
2 item B,<u>1 2b1</u> black<u>2 2b2</u> etc,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
2 item B 1,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
2 item B 2,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
3 item B,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
The regular expression (?:^[^,]+,.*?<u>(\d{1,2} .+?)</u>[^,]*,) does match, but the challenge is how to get the multiple matches and place them at the desired location in all respective major and sub items. Can anyone help with this? Thanks already.