正则表达式字符出现之间的匹配 [英] Regular Expression Match between occurrence of character
问题描述
我有以下字符串:
3#White House, District Of Columbia, United States#US#USDC#DC001#38.8951#-77.0364#531871#382
如您所见,字符串由#分隔.我的用例类似于一个简单的SPLIT(string,#")操作,但是regex给了我更多的灵活性.
as you can see, the string is delimited by #'s. My use-case resembles a simple SPLIT(string,"#") operation but regex gives me a bit more flexibility.
我想匹配两次出现的#之间的字符.例如,第二次和第三次出现之间的字符应匹配:"US"
I would like to match the characters between two occurrences of #'s. for example the characters between the second and third occurrence should match: 'US'
我使用的是Google Bigquery,能够匹配字符串的前两个词,但是却与第三个词不符:
I'm using Google Bigquery and was able to match the first two terms of the string but struggle with the third:
REGEXP_EXTRACT(locations,r'^\d') as location_type,
REGEXP_REPLACE(REGEXP_EXTRACT(locations,r'^\d#.*?#'),r'^\d*#|#','') as location_full_name,
????
位置是字符串,例如上面的一个.
locations are strings such as the one above.
I've found this question but I have multiple delimeters and would like to specify between which occurences the match should take place e.g. 2 and 5th occurrence.
推荐答案
您可以使用^(?:[^#]*#){N}([^#]*)
这样的正则表达式,其中N
是所需的子字符串的数量减去1.要获取US
,这是第三个值,您可以使用
You may use a regex like ^(?:[^#]*#){N}([^#]*)
where N
is the number of your required substring minus 1. To get US
, which is the third value, you may use
^(?:[^#]*#){2}([^#]*)
请参见 regex演示
详细信息
-
^
-字符串开头 -
(?:[^#]*#){2}
-两个序列-
[^#]*
-除#
之外的任何零个或多个字符
-
#
-一个#
字符
^
- start of string(?:[^#]*#){2}
- two sequences of[^#]*
- any zero or more chars other than#
#
- a#
char
这篇关于正则表达式字符出现之间的匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-