如何删除某些斜杠之间的string正则expression式或Excel
我正在寻找一种方法来删除string后第三和第四个正斜杠
例如http://www.website.com/content/remove-this/product
到http://www.website.com/content/product
我可以使用记事本++,正则expression式或Excel
我试过使用
/.*?/(.*?)/
但是这并没有奏效
尝试使用记事本++与“replace”和使用expression式
^(.*://)([^/]*/)([^/]*/)([^/]*/)(.*)$
并replace为
$1$2$3$5
对于使用Excel的答案:
式
=LEFT(A1,FIND(CHAR(1),SUBSTITUTE(A1,"/",CHAR(1),4)))&MID(A1,1+FIND(CHAR(1),SUBSTITUTE(A1,"/",CHAR(1),5)),99)
UDF (使用正则expression式)
Option Explicit Function Remove4th(S As String) As String Dim RE As Object Set RE = CreateObject("vbscript.regexp") With RE .Global = True .Pattern = "^((?:.*?/){4})[^/]*/" .MultiLine = True Remove4th = .Replace(S, "$1") End With End Function
我会做这样的事情:
<?php $string = " http://www.website.com/content/remove-this/product"; preg_match_all('#http:\/\/([a-zA-Z0-9-.]*)\/([a-zA-Z0-9-]*)\/([a-zA-Z0-9-]*)\/([a-zA-Z0-9-]*)#ism',$string,$out); $new_string = 'http://'.$out[1][0].'/'.$out[4][0]; echo $new_string; // => http://www.website.com/content ?>