如何精确搜索和替换(How to search and replace precisely)
我有一个文本文件,其内容非常类似于以下内容
G12
G10,11
GO12
GO10,12
我要做的是将所有'G'替换为'',而不是将'GO'替换为'O'。
我正在尝试使用正则表达式,所以首先我处理的是没有逗号的行
with open(file) as f: for line in f: if re.match(r"%s[0-9]+" % 'G', line): input = line.strip('G')
事实证明,“G10,11 \ n”字符串也匹配正则表达式模式,这让我感到困惑。
我的陈述是否有任何问题,或者有更简单的方法吗? 非常感谢,
I have a text file which content is quite like following
G12
G10,11
GO12
GO10,12
What I am trying to do is replace all the 'G' with '' but not replacing 'GO' to 'O'.
I was trying to do with regular expressions, so first I am dealing with the lines without a comma
with open(file) as f: for line in f: if re.match(r"%s[0-9]+" % 'G', line): input = line.strip('G')
And it turns out that the "G10,11\n" string is matching the regex pattern either, which makes me confused.
Is there any wrong with my statement, OR is there any simpler approach to do this? Many thanks,
原文:https://stackoverflow.com/questions/21983626
最满意答案
尝试这个:
Dim sourceSht As Worksheet Dim myrange As Range Dim DestRow As Integer Dim ws2 As Worksheet Dim MonthVal As String Dim rw As Range Range("D3").Select ActiveCell.FormulaR1C1 = "=MONTH(R[-1]C)" Range("D3").Select Selection.NumberFormat = "General" MonthVal = ActiveCell.Value Set sourceSht = ThisWorkbook.Worksheets("Signed") Set myrange = sourceSht.Range("Y1:Y" & sourceSht.UsedRange.Row - 1 + sourceSht.UsedRange.Rows.Count) Set ws2 = Sheets("April") DestRow = ws2.Cells(Rows.Count, "A").End(xlUp).Row ' + 1 For Each rw In myrange '.Rows If sourceSht.Cells(rw.Row, 12).Value = MonthVal Then DestRow = DestRow + 1 rw.Copy Destination:=Sheets("April").Range("A" & DestRow) End If Next rw
编辑:修复第15行的潜在漏洞(从
Set myrange = sourceSht.Range("Y1:y" & sourceSht.UsedRange.Rows.Count)
到Set myrange = sourceSht.Range("Y1:Y" & sourceSht.UsedRange.Row - 1 + sourceSht.UsedRange.Rows.Count)
),以防顶部有未使用的行。Try this:
Dim sourceSht As Worksheet Dim myrange As Range Dim DestRow As Integer Dim ws2 As Worksheet Dim MonthVal As String Dim rw As Range Range("D3").Select ActiveCell.FormulaR1C1 = "=MONTH(R[-1]C)" Range("D3").Select Selection.NumberFormat = "General" MonthVal = ActiveCell.Value Set sourceSht = ThisWorkbook.Worksheets("Signed") Set myrange = sourceSht.Range("Y1:Y" & sourceSht.UsedRange.Row - 1 + sourceSht.UsedRange.Rows.Count) Set ws2 = Sheets("April") DestRow = ws2.Cells(Rows.Count, "A").End(xlUp).Row ' + 1 For Each rw In myrange '.Rows If sourceSht.Cells(rw.Row, 12).Value = MonthVal Then DestRow = DestRow + 1 rw.Copy Destination:=Sheets("April").Range("A" & DestRow) End If Next rw
Edit: fixed a potential loophole at line 15 (from
Set myrange = sourceSht.Range("Y1:y" & sourceSht.UsedRange.Rows.Count)
toSet myrange = sourceSht.Range("Y1:Y" & sourceSht.UsedRange.Row - 1 + sourceSht.UsedRange.Rows.Count)
), in case there are unused rows at the top.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
尝试这个: Sub copy_original()Dim filteredRng As Range,cl As Range,rw As Integer Application.DisplayAlerts = False Application.AlertBeforeOverwriting = False Application.ScreenUpdating = True With ThisWorkbook.Worksheets("Sheet1") .AutoFilterMode = False ...
-
已经更新并添加了一个表单变量(ws),它指向相关工作表进行复制(不需要选择或激活)。 Sub Summary() Dim wb1 As Workbook Dim Sht As Worksheet Dim Rng, Rng2 As Range Dim wb2 As Workbook Dim cell As Range Dim ws as Worksheet Set wb1 = ThisWorkbook Set wb2 = Workbo ...
-
你想做这样的事吗? Private Sub Worksheet_Change(ByVal Target As Range) Dim val As String val = Sheets("Sheet2").Range("B2").Value If Target.Column = 6 And (Target.Row >= 1 And Target.Row <= 10000) Then 'Whatever column Sheets("Sheet1").R ...
-
尝试使用此代码。 希望能帮助到你: Sub test() Dim sh1 As Worksheet Dim sh2 As Worksheet Dim lastrow1 As Long Dim lastrow2 As Long Dim j As Long Dim i As Long Dim rng As Range 'set correct name of the sheet with your data' Set sh1 = ThisWorkbook. ...
-
这样做你想要的: Sub copyABunchOfThings() Dim i As Integer, j As Integer i = 2 j = 4 For i = 1 To 10 'Change 1 and 10 into your numbers. If Sheet1.Cells(i, 4) <> 0 And Sheet1.Cells(i, 49) <> 0 Then Sheet2.Cells(j, 1).Value = "FK" ...
-
如果它们在同一工作表中包含单词“save”到列I,则从列A移动单元格 你的代码没有做这样的事情。 要满足您的要求,您需要这样的东西: Sub Findandcut() Dim row As Long For row = 2 To 1000 ' Check if "save" appears in the value anywhere. If Range("A" & row).Value Like "*save*" Then ' Cop ...
-
遍历列中的单元格,如果包含条件,则将行复制到新工作表(Iterate through cells in a column, if contains criteria, copy row to new sheet)[2022-08-25]
我知道在问之前我应该发布我的尝试。 无论如何,我想出了如何做到这一点。 我是VBA的新手,但这有效: Set mainSht = Worksheets("Sheet1") ' Copy From this sheet 'Find the last row with data in specified column. LR = mainSht.Cells(Rows.Count, "F").End(xlUp).Row Set LRange = mainSht.Range("F2:F" & LR) 'Fi ... -
尝试这个: Dim sourceSht As Worksheet Dim myrange As Range Dim DestRow As Integer Dim ws2 As Worksheet Dim MonthVal As String Dim rw As Range Range("D3").Select ActiveCell.FormulaR1C1 = "=MONTH(R[-1]C)" Range("D3").Select Selection.NumberFormat = "General" Mon ...