0

私はURLを持っています:

url = "http://timesofindia.feedsportal.com/fy/8at2EuL0ihSIb3s7/story01.htmA"

最後に A、TRE などの不要な文字がいくつかあります。これを削除したいので、URLは次のようになります。

url = http://timesofindia.feedsportal.com/fy/8at2EuL0ihSIb3s7/story01.htm

どうすれば削除できますか?

4

1 に答える 1

2

URL が常に で終わる場合.htm.apsxまたは.php単純な正規表現で解決できる場合:

url = url[/^(.+\.(htm|aspx|php))(:?.*)$/, 1]

ここ Rubular でテストします。

まず、このメソッドを使用して部分文字列を取得します。スライスのように機能します。次に正規表現です。左から右へ:

^                   # Start of line
  (                   # Capture everything wanted enclosed
    .+                  # 1 or more of any character
    \.                  # With a dot after it
    (htm|aspx|php)      # htm or aspx or php
  )                   # Close url asked in question
  (                   # Capture undesirable part
    :?                  # Optional
    .*                  # 0 or more any character
  )                   # Close undesirable part
$                   # End of line
于 2013-01-10T12:15:55.840 に答える