簡體   English   中英

替換 2 個字符之間的值

[英]Replacing value between 2 characters

url="https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-167x378.jpg"

我有這個網址,我想用920*458替換167 * 378 這基本上是圖像的尺寸。 我試過這個代碼:

url.replace("/-.*./","920x458")

但 url 沒有任何變化。 我想要的輸出是:

"https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-950x458.jpg"

我怎樣才能做到這一點?

我的觀點是,字符串替換更具可讀性和明顯性,在失敗時也更容易調試。 在大多數情況下它比 regexp 更快!

def change_url_resolution(url: str, x: int, y: int) -> str: 
    """
    Changes the resolution in the url 
    >>> url="https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-167x378.jpg"
    >>> change_url_resolution(url, 920, 458)
    'https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-920x458.jpg'
    """
    # we only need the part left of the rightmost '-'
    url_prefix = url.rsplit('-',1)[0]
    # all the rest we can construct ourself
    result = ''.join([url_prefix, '-', str(x), 'x', str(y), '.jpg'])
    # or if You like it better (from python 3.6) : 
    result = f"{url_prefix}-{x}x{y}.jpg"
    return result

您可以嘗試以下正則表達式:

(?<=-)\d+x\d+

細節:

  • (?<=-) :在字符連字符后只能得到文本
  • \\d+x\\d+ :您獲得當前圖像大小

我試過在 Python 上運行

import re

url = "https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-167x378.jpg"

print("url before replacing")
print(url)
url = re.sub(r"(?<=-)\d+x\d+", "920x458", url)
print("url after replacing")
print(url)

結果我得到了什么:

url before replacing
https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-167x378.jpg
url after replacing
https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-920x458.jpg

您可以使用re.sub並匹配連字符和\\d+匹配一位或多位數字的維度,而不是使用string.replace

-\d+x\d+

正則表達式演示

並替換為-920x458


如果尺寸應以.jpg結尾,您可以使用正向前瞻(?=\\.jpg$)來斷言它緊跟在尺寸之后直到字符串末尾。

如果.jpg應該是更廣泛的匹配,您也可以使用\\.\\w+來匹配 1 個或多個單詞字符。

-\d+x\d+(?=\.jpg$)

正則表達式演示| Python 演示

示例代碼

import re

regex = r"-\d+x\d+(?=\.jpg$)"
url="https://e...content-available-to-author-only...a.com/wp-content/uploads/2020/08/abc_def_ghi-167x378.jpg"
print(re.sub(regex, "-920x458", url))

輸出

https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-920x458.jpg

你可以使用 Python 的默認re模塊:

import re
url = 'https://ensia.com/wp-content/uploads/2020/08/abc_def_ghi-167x378.jpg'
new_url = re.sub('[0-9]{3}x[0-9]{3}', '950x458', url)
  • [0-9]匹配從 0 到 9 的任何數字。
  • [0-9]{3}表示從 0 到 9 出現 3 次的任何數字(例如 123)。

re.sub(r'(?<=-)\d+x\d+(?=\.[^.]*$)', r'920x458', url)

證明 它只會在文件擴展名之前重新re.sub文件名末尾的數字。

解釋

                        EXPLANATION
--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    -                        '-'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  x                        'x'
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    [^.]*                    any character except: '.' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM