简体   繁体   English

如何从字符串中选择并裁剪某些字符?

[英]How can I select and crop certain characters from a string?

For this code, I am having a problem in using the MID and INSTR functions: 对于此代码,我在使用MID和INSTR函数时遇到问题:

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set file = objFSO.OpenTextFile("sample.txt" , ForReading)  
Const ForReading = 1

Dim re
Dim controller
Dim action
Set re = new regexp 
re.pattern = "(contextPath\s*?[+]\s*?[""][/]\w+?[?]action[=]\w+?[""])"
re.IgnoreCase = True
re.Global = True

Dim line
Do Until file.AtEndOfStream
    line = file.ReadLine
    For Each m In re.Execute(line)

        var = m.Submatches(0)

        'I am having a problem with the next two lines:

        controller = Mid(var, 1, InStr(var, "contextPath\")) & "[?]action[=]\w+?[""]n"
        action = Mid(var, 1, InStr(var, "contextPath\s*?[+]\s*?[""][/]\w+?[?]action[=]")) & """"

        Wscript.Echo "controller :" & controller
        Wscript.Echo "action: " & action
    Next
Loop

With the text file "sample.txt": 使用文本文件“ sample.txt”:

contextPath+"/GIACOrderOfPaymentController?action=showORDetails"
contextPath +"/GIACPremDepositController?action=showPremDep"
contextPath+ "/GIACCommPaytsController?action=showCommPayts"

(Notice the spaces beside the plus(+) sign) (请注意加号(+)旁边的空格)

How can I make the output look like this: 如何使输出看起来像这样:

controller: GIACOrderOfPaymentController
controller: GIACPremDepositController
controller: GIACCommPaytsController

action: showORDetails
action: showPremDep
action: showCommPayts

Instead of capturing the full line, capture the needed data 而不是捕获全部行,而是捕获所需的数据

Option Explicit

Const ForReading = 1

Dim re
    Set re = New RegExp
    With re 
        .Pattern = "contextPath\s*\+\s*\""/(\w+)\?action=(\w+)\"""
        .IgnoreCase = True 
        .Global = True 
    End With

Dim controllers, actions
    Set controllers = CreateObject("Scripting.Dictionary")
    Set actions = CreateObject("Scripting.Dictionary")

Dim file
    Set file = CreateObject("Scripting.FileSystemObject").OpenTextFile("sample.txt" , ForReading)  

Dim line, m
    Do Until file.AtEndOfStream
        line = file.ReadLine
        For Each m In re.Execute(line)
            controllers.Add "K" & controllers.Count, m.Submatches(0)
            actions.Add "K" & actions.Count, m.Submatches(1)
        Next
    Loop

Dim item

    For Each item in controllers.Items()
        WScript.Echo "controller: " & item
    Next 

    WScript.Echo ""

    For Each item in actions.Items()
        WScript.Echo "action: " & item
    Next 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从字符串中删除某些字符? - How can I remove certain characters from a string? 如何从具有特定条件的字符串中选择特定字符 - How to select certain characters from a string with a certain condition 如何检查多个特定字符的字符串? - How can I check a string for multiple certain characters? 如何将字符串的某些字符转换为 Java 中的浮点数? - How can I convert certain characters of a String into a Float in Java? 如何对包含特定字符串的 select 文件名使用正则表达式? - How can I use regex to select filenames that contain a certain string? 如何匹配字符串的某个部分中的某些字符与正则表达式? - How do I match certain characters from a certain part of string with a regex? 如何使用Regex解析不规则CSV而不选择某些字符 - How can I use Regex to parse irregular CSV and not select certain characters 如何从尖括号内删除某些字符,而使字符独自留在外面? - How can I remove certain characters from inside angle-brackets, leaving the characters outside alone? 使用JavaScript,如何在保留特定字符的情况下以一定长度将字符串断开? - Using JavaScript how can I break a string at a certain length, preserving special characters? 如何编写仅在字符串包含某些字符和数字时才能激活的正则表达式? - How can I write a regular expression that is only activated if the string contains certain characters and numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM