正則表達式的最后一部分字符串不一致的模式或長度

[英]Regex last part of string not consistent pattern or length


I'm trying to write a regex style condition to only extract the last part of a long string. I'm having trouble getting it to work as the string format is not consistent. I've included an example below:

我正在嘗試編寫一個正則表達式樣式條件,只提取長字符串的最后一部分。由於字符串格式不一致,我無法使其正常工作。我在下面列舉了一個例子:

2:0000:PlaceOne|2:30000:PlaceTwo|187768:20003:PlaceThree|187904:20011:PlaceFour|2614991:20033:PlaceFive|1166533:60006:PlaceSix

In this example what I need the output to be is PlaceSix. In other examples it could be PlaceFive, PlaceSeven etc so the length is not always the same for the entire string or for what I'm extracting. The only consistent pattern is it will always be at the end of the string and comes after the last colon (all characters after that colon). I'm sure this must be possible with regex but so far regrettably have been unable to get this to work.

在這個例子中,我需要輸出的是PlaceSix。在其他示例中,它可能是PlaceFive,PlaceSeven等,因此對於整個字符串或我正在提取的內容,長度並不總是相同。唯一一致的模式是它始終位於字符串的末尾,並且位於最后一個冒號之后(該冒號之后的所有字符)。我確信這一切必須可以使用正則表達式,但到目前為止,令人遺憾的是無法使其正常工作。

Also expanding on this logic I have a related question, if it is possible to do this via regex, can I also use the same logic to extract another part of the string if needed? So for instance for the same example above, if I wanted to extract PlaceTwo instead is that possible? Problem is there is no consistent pattern I can think of, not even being at the end of the string like with the previous PlaceSix example. It does come after a colon but as you can see there are multiple colons within the string. The numbers will also seem random corresponding to the places. Was thinking maybe something along the lines of after x number of colons extract text inbetween: and | although I'm not even sure if that's actually possible.

同樣擴展這個邏輯我有一個相關的問題,如果可以通過正則表達式這樣做,我是否也可以使用相同的邏輯來提取字符串的另一部分,如果需要的話?因此,例如對於上面的相同示例,如果我想提取PlaceTwo,那可能嗎?問題是沒有我能想到的一致模式,甚至不像前一個PlaceSix示例那樣位於字符串的末尾。它確實在冒號之后,但正如您所看到的,字符串中有多個冒號。對於這些地方,這些數字也似乎是隨機的。在考慮x之后的冒號提取文本之間的某些東西:和雖然我甚至不確定這是否真的可行。

Main question is the first question though, if that's possible I'll consider this question answered. The second part is more of a bonus question if the first one is possible, being so similar didn't think it was worth posting two separate questions.

主要問題是第一個問題,如果可能,我會考慮回答這個問題。如果第一部分是可能的話,第二部分更多的是獎金問題,如此相似並不認為值得發布兩個單獨的問題。

Hopefully I've explained this correctly, please let me know if any further clarifications are required. Many thanks.

希望我已經正確解釋了這一點,如果需要進一步澄清,請告訴我。非常感謝。

2 个解决方案

#1


1  

How's this

:([^:]*)$

You can test it here

你可以在這里測試一下

And to answer your other question, i would say that as long as you know what it is you are looking for, there should be a way to find it.

回答你的另一個問題,我會說,只要你知道你在尋找什么,就應該有辦法找到它。

To give an example, if i want to get the content after the fourth : every single time, its as simple as:

舉個例子,如果我想獲得第四個之后的內容:每一次,它簡單如下:

^(?:[^:]*:){4}([^:]*)

And to change the position, all you have to do is change the 4 to whatever you want

要改變立場,你所要做的就是將4改為你想要的任何東西

Test it here

在這里測試一下

#2


1  

drop table t;
create table t (str varchar(1000));
insert into t (str) values ('2:0000:PlaceOne|2:30000:PlaceTwo|187768:20003:PlaceThree|187904:20011:PlaceFour|2614991:20033:PlaceFive|1166533:60006:PlaceSix
');

select  split_part(split_part(str,'|',1),':',3) as c1
       ,split_part(split_part(str,'|',2),':',3) as c2
       ,split_part(split_part(str,'|',3),':',3) as c3
       ,split_part(split_part(str,'|',4),':',3) as c4
       ,split_part(split_part(str,'|',5),':',3) as c5
       ,split_part(split_part(str,'|',6),':',3) as c6

from    t
;

+----------+----------+------------+-----------+-----------+----------+
| c1       | c2       | c3         | c4        | c5        | c6       |
+----------+----------+------------+-----------+-----------+----------+
| PlaceOne | PlaceTwo | PlaceThree | PlaceFour | PlaceFive | PlaceSix |
+----------+----------+------------+-----------+-----------+----------+

select  arr[1*3] as c1   
       ,arr[2*3] as c2
       ,arr[3*3] as c3
       ,arr[4*3] as c4
       ,arr[5*3] as c5
       ,arr[6*3] as c6

from   (select  regexp_split_to_array(str,'[|:]') as arr
        from    t
        ) t

+----------+----------+------------+-----------+-----------+----------+
| c1       | c2       | c3         | c4        | c5        | c6       |
+----------+----------+------------+-----------+-----------+----------+
| PlaceOne | PlaceTwo | PlaceThree | PlaceFour | PlaceFive | PlaceSix |
+----------+----------+------------+-----------+-----------+----------+

select  arr[1+1] as c1   
       ,arr[2+1] as c2
       ,arr[3+1] as c3
       ,arr[4+1] as c4
       ,arr[5+1] as c5
       ,arr[6+1] as c6 

from   (select  regexp_split_to_array('|'||str,'\|([^:]+:){2}') as arr
        from    t
        ) t

+----------+----------+------------+-----------+-----------+----------+
| c1       | c2       | c3         | c4        | c5        | c6       |
+----------+----------+------------+-----------+-----------+----------+
| PlaceOne | PlaceTwo | PlaceThree | PlaceFour | PlaceFive | PlaceSix |
+----------+----------+------------+-----------+-----------+----------+

select  arr[1] as c1 
       ,arr[2] as c2
       ,arr[3] as c3
       ,arr[4] as c4
       ,arr[5] as c5
       ,arr[6] as c6

from   (select  regexp_matches(str,'^.*?:([^:|]*)\|.*?:([^:|]*)\|.*?:([^:|]*)\|.*?:([^:|]*)\|.*?:([^:|]*)\|.*?:([^:|]*)$') as arr               
        from    t
        ) t

+----------+----------+------------+-----------+-----------+----------+
| c1       | c2       | c3         | c4        | c5        | c6       |
+----------+----------+------------+-----------+-----------+----------+
| PlaceOne | PlaceTwo | PlaceThree | PlaceFour | PlaceFive | PlaceSix |
+----------+----------+------------+-----------+-----------+----------+

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/02/01/720adf613fe674def1b7c169641d3297.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com