从excel文件中的所有列中删除部分文本

时间:2014-10-17 14:47:04

标签: excel text

我有一个excel文件,其中包含一个显示Web源URL的列。数据看起来像这样......

http://carter.mydomain.com/page1

http://expert.mydomain.com/page4

http://10629.mydomain.com/sample

http://genius.mydomain.com/form-1

我需要做的是删除(http://)之前的所有内容以及第一个(。)之后的所有内容 因此,在前面的示例中,我希望在列

中留下以下数据

卡特

专家

10629

天才

提前感谢您对此的任何帮助。 卡伦

2 个答案:

答案 0 :(得分:0)

您可以使用此类公式的组合。这假设A1包含卡特的第一行。

=MID(A1,FIND("//",A1)+2,FIND(".",A1,FIND("//",A1))-FIND("//",A1)-2)

答案 1 :(得分:0)

这是一个使用正则表达式搜索/替换的暴力宏,遗憾的是它不会自动成为Excel内置的搜索/替换功能的一部分。提示搜索/替换模式而不是硬编码会更有用,但是你会得到这个想法,因为这只是使用正则表达式的一个例子。请注意,您可能必须首先将正则表达式引用添加到工作簿中。

要使用,请选择范围,然后在添加后运行宏。

Sub SearchReplaceRegex()
  Dim reg As New RegExp  ' regex object
  Dim searchPattern1     ' look for this
  Dim searchPattern2
  Dim replacePattern1    ' replace with this
  Dim replacePattern2
  Dim matchCell As Range ' selected cell

  ' Set search/replace patterns
  searchPattern1 = "^http://" ' http:// at the beginning of the line
  searchPattern2 = "\..*$"    ' 1st period followed by anything until the end of the line
  replacePattern1 = ""        ' replace with nothing
  replacePattern2 = ""

  ' regex object settings
  reg.IgnoreCase = True
  reg.MultiLine = False

  ' Loop through each selected cell
  For Each matchCell In Selection

    ' Does it match the first pattern?
    reg.Pattern = searchPattern1
    If reg.Test(matchCell) Then
      ' If so, replace the matched search pattern with the replace pattern
      matchCell = reg.Replace(matchCell, replacePattern1)
    End If

    ' Change to the second pattern and test again
    reg.Pattern = searchPattern2
    If reg.Test(matchCell) Then
      matchCell = reg.Replace(matchCell, replacePattern2)
    End If

  Next

End Sub

有关详细信息,请参阅此帖子,其中包含大量有用信息:How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops