检索两个特殊字符之间的字符串

时间:2023-02-07 17:07:18

I have an image URL which I need to extract the UUID from. The URL is formed like so:

我有一个图像URL,我需要从中提取UUID。URL的格式如下:

"https://firebasestorage.googleapis.com/v0/b/project-6312172760840445565.appspot.com/o/images%2Fjohn%2F356BEDA3-E0B2-4659-9DE2-37FC8C13CDF7.webp?alt=media&token=f0c16de2-c1d9-4392-8f5b-9aa5c0527ea3"

This URL points to the image in my bucket, where its name format is /images/[username]/[UUID]. I need to obtain this UUID in order to delete it so it doesn't take up space when the user uploads a new image (which replaces the existing one).

这个URL指向我的bucket中的映像,它的名称格式是/images/[username]/[UUID]。我需要获得这个UUID,以便删除它,这样当用户上传一个新的图像(替换现有的图像)时,它就不会占用空间。

I tried testing this here with the following regex: \%([A-Z0-9-]+)\. Essentially, I want to extract all the text between a %2F and a period (.), which is where the UUID is (356BEDA3-E0B2-4659-9DE2-37FC8C13CDF7). It successfully matched the pattern I wanted (under "match information")...well, including the 2F which is actually an escaped slash.

我在这里尝试使用以下regex: \%([A-Z0-9-]+)\进行测试。本质上,我想提取%2F和句点(.)之间的所有文本,即UUID所在的位置(356beda3 - e0b2 -4659- 9de2 - 37fc13cdf7)。它成功地匹配了我想要的模式(在“匹配信息”下)……包括2F实际上是一个转义的斜线。

However, when using it in Swift, it's actually matching the characters including and after the first % (output is %2Fj). I've tried this:

然而,当在Swift中使用它时,它实际上是在匹配第一个%之后的字符(输出是%2Fj)。我已经试过这个:

let regex = Regex("%([A-Z0-9-]+).")

if let match = regex.match(self.existingItem.photoURL) {
    print(match.matchedString)
}

Side note: I'm using this Regex framework.

附注:我正在使用这个Regex框架。

Can anyone please shed some insight on why this happens in Swift and how to fix it?

谁能解释一下为什么这种情况会迅速发生,以及如何解决?

2 个解决方案

#1


1  

You need the Capture:

你需要截图:

let reg = Regex("%2F([A-Z0-9-]+)[.]")

if let res = reg.match(self.existingItem.photoURL)?.captures[0] {
  print("\(res)")
}

Note that the (...) creates a capture group that is used to get smaller substrings from your matches.

注意(…)创建一个捕获组,用于从匹配中获取更小的子字符串。

Judging by the Sharplet Regex Github page, capture group indexing starts with 0, so the first capture group can be accessed via 0 index.

根据Sharplet Regex Github页面的判断,捕获组索引从0开始,因此可以通过0索引访问第一个捕获组。

#2


2  

@rock321987 is right, you've got to escape the dot:

@rock321987是对的,你必须逃离这个点:

%([A-Z0-9-]+)\\.

FWIW, it works like this without using a third-party framework:

FWIW的工作方式是这样的,没有使用第三方框架:

if let range = text.rangeOfString("%([A-Z0-9-]+)\\.", options: .RegularExpressionSearch) {
    let match = text.substringWithRange(range)
    print(match)
}

Prints:

打印:

%2F356BEDA3-E0B2-4659-9DE2-37FC8C13CDF7.

% 2 f356beda3 - e0b2 - 4659 - 9 -德- 37 - fc8c13cdf7。

#1


1  

You need the Capture:

你需要截图:

let reg = Regex("%2F([A-Z0-9-]+)[.]")

if let res = reg.match(self.existingItem.photoURL)?.captures[0] {
  print("\(res)")
}

Note that the (...) creates a capture group that is used to get smaller substrings from your matches.

注意(…)创建一个捕获组,用于从匹配中获取更小的子字符串。

Judging by the Sharplet Regex Github page, capture group indexing starts with 0, so the first capture group can be accessed via 0 index.

根据Sharplet Regex Github页面的判断,捕获组索引从0开始,因此可以通过0索引访问第一个捕获组。

#2


2  

@rock321987 is right, you've got to escape the dot:

@rock321987是对的,你必须逃离这个点:

%([A-Z0-9-]+)\\.

FWIW, it works like this without using a third-party framework:

FWIW的工作方式是这样的,没有使用第三方框架:

if let range = text.rangeOfString("%([A-Z0-9-]+)\\.", options: .RegularExpressionSearch) {
    let match = text.substringWithRange(range)
    print(match)
}

Prints:

打印:

%2F356BEDA3-E0B2-4659-9DE2-37FC8C13CDF7.

% 2 f356beda3 - e0b2 - 4659 - 9 -德- 37 - fc8c13cdf7。