[英]Remove last character from the first word in a string
I'm using RegEx to search for the string 我正在使用RegEx来搜索字符串
If RegExp.test(Cel.Value) Then
Debug.Print Left$(Cel.Value, Len(Cel.Value) - 1)
End If
This is my Data 这是我的数据
3250A WEST SEM
110055K KEALY RD
804B WEST AMERICA
804 EAST AMERICA
Result should be 结果应该是
3250 WEST SEM
110055 KEALY RD
804 WEST AMERICA
804 EAST AMERICA
What is the best way to delete the last character? 删除最后一个字符的最佳方法是什么?
You can use this regex: 你可以使用这个正则表达式:
(\d+)(\w{1})(\s.+)
(\\d+)
- capture one or more digits (\\d+)
- 捕获一个或多个数字 (\\w{1})
- capture one letter that you want to remove - if you have examples 123XX SOMEWHERE then to capture XX you would use (\\w+)
or (\\w{1,2})
if you think there will be 1, or 2 characters following the number. (\\w{1})
- 捕获一个你要删除的字母 - 如果你有示例123XX SOMEWHERE然后捕获XX你会使用(\\w+)
或(\\w{1,2})
如果你认为会有数字后面有1个或2个字符。 (\\s.+)
- capture a space followed by anything (\\s.+)
- 捕获一个空格,后跟任何东西 When you want to replace you just want to remove the second match group ( (\\w{1})
) and join the first and third - hence the $1$3
in the Replace
function below: 当您想要替换时,您只想删除第二个匹配组( (\\w{1})
)并加入第一个和第三个 - 因此下面的Replace
函数中的$1$3
:
strReplaced = objRegex.Replace(CStr(varTest), "$1$3")
Example VBA code 示例VBA代码
Option Explicit
Sub Test()
Dim objRegex As Object
Dim varTests As Variant
Dim varTest As Variant
Dim strPattern As String
Dim strReplaced As String
varTests = Array("3250A WEST SEM", "110055K KEALY RD", "804B WEST AMERICA")
strPattern = "(\d+)(\w{1})(\s.+)"
Set objRegex = CreateObject("VBScript.Regexp")
objRegex.Pattern = strPattern
For Each varTest In varTests
strReplaced = objRegex.Replace(CStr(varTest), "$1$3")
MsgBox strReplaced
Next varTest
End Sub
Fancy diagram of regex: 正则表达式的花式图:
RegEx scenario - If you can find <some numbers><single letter><single space> at the beginning of the string, then change the pattern to <single letter><single space> and replace it with a single space. RegEx场景 - 如果您可以在字符串的开头找到<some numbers> <single letter> <single space> ,则将模式更改为<single letter> <single space>并将其替换为单个空格。
Option Explicit
Sub stripAlphaSuffix()
Dim i As Long, regex As Object
Set regex = CreateObject("VBScript.RegExp")
With Worksheets("Sheet1")
For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
With regex
.Global = False
.MultiLine = False
.IgnoreCase = False
.Pattern = "^[0-9A-Z ]{3,8}"
End With
If regex.Test(.Cells(i, "A").Value2) Then
regex.Pattern = "[A-Z ]{2}"
.Cells(i, "A") = regex.Replace(.Cells(i, "A").Value2, Chr(32))
End If
Next i
End With
End Sub
just to throw in a not-RegEx solution: 只是为了投入一个非RegEx解决方案:
Dim cel As Range
Dim word As String
For Each cel In Range("A1", Cells(Rows.Count, 1).End(xlUp))
word = Split(cel, " ")(0)
cel.Value = Replace(cel.Value, word, Left(word, Len(word) - 1))
Next
As VAL
strips non numerics off a alphanumeric string starting with a number then you could try this (which doesn't presume that there is always a character to be replaced) 当VAL
从字母数字字符串中删除非数字时,你可以尝试这个(这并不是假设总有一个字符要被替换)
VAL("3250A")
=3250
VAL("3250A")
=3250
Would be more elegant if it didn't need to append the rest of the string :) 如果它不需要追加其余的字符串会更优雅:)
Dim StrIn As String
Dim X
StrIn = "3250A WEST SEM"
X = Split(StrIn, Chr(32))
MsgBox Val(StrIn) & Right$(StrIn, Len(StrIn) - Len(X(0)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.