Two Regexp in a loop ASP - regex

Function VeriBul(BaslangicDegeri,BitisDegeri)
Set RegEx = New RegExp
RegEx.IgnoreCase = True
RegEx.Global = True
RegEx.Pattern = "(" & BaslangicDegeri & ")(.+?)(" & BitisDegeri & ")"
Set Sonuc = RegEx.Execute(Mid(strRetval,29600,254000))
For Each i in Sonuc
VeriBul = VeriBul & tr2(i.SubMatches(1))
Set RegEx2 = New RegExp
RegEx2.IgnoreCase = True
RegEx2.Global = True
RegEx2.Pattern = "(<hemenalfiyat>)(.+?)(</hemenalfiyat>)"
Set Sonucx = RegEx2.Execute(Mid(strRetval,33000,2000))
For Each y in Sonucx
VeriBul2 = VeriBul2 & tr2(y.SubMatches(1))
Next
When it works then appering error
Next
======================================================
For Each i in Sonuc
i.SubMatches(1)
For Each y in Sonucx
y.SubMatches(1)
Next
Next
How I can print this except Submatches code? because I have two regexp code (so Im used as Regex2) like that and I can't use to Submatches again.
Thanks,

Difficult to fix this without the rest of your code (tr2) and your input and desired output but i gues your code should be something like this
Function VeriBul(BaslangicDegeri,BitisDegeri)
Set RegEx = New RegExp
RegEx.IgnoreCase = True
RegEx.Global = True
RegEx.Pattern = "(" & BaslangicDegeri & ")(.+?)(" & BitisDegeri & ")"
Set RegEx2 = New RegExp
RegEx2.IgnoreCase = True
RegEx2.Global = True
RegEx2.Pattern = "(<hemenalfiyat>)(.+?)(</hemenalfiyat>)"
Set Sonuc = RegEx.Execute(Mid(strRetval,29600,254000))
Set Sonucx = RegEx2.Execute(Mid(strRetval,33000,2000))
For Each i in Sonuc
VeriBul = VeriBul & tr2(i.SubMatches(1))
Next
For Each y in Sonucx
VeriBul = VeriBul & tr2(y.SubMatches(1))
Next
Next

Related

How do I remove the dot at the end of each substring

In trying to remove the dot at the end of each substring A6.3., A6.5. I came up with the following solution. However I get a run-time error "5017"
This is what I have
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "(?<=[1-9]\.[1-9])\."
RemoveNumbers = .Replace("A6.3., A6.5. ", "")
End With
MsgBox RemoveNumbers
The outcome should be: A6.3, A6.5
Just remove dot without regex:
Sub RemoveLastDot()
myValues = Split("A6.3., A6.5.", ",")
myRslt = ""
myFirst = ""
For Each myVal In myValues
If InStrB(Trim(myVal), ".") = Len(Trim(myVal)) Then
myRslt = myRslt & myFirst & Left(Trim(myVal), Len(Trim(myVal)) - 1)
else
myRslt = myRslt & myFirst & Trim(myVal)
End If
myFirst = ", "
Next
MsgBox myRslt
End Sub

Regex not Triggering VBA

This is my regex:
Dim vbRegX As Object, vbRegXMatch As Object
Set vbRegX = CreateObject("vbscript.regexp")
With vbRegX
.Global = True
.IgnoreCase = True
.Pattern = "^[a-zA-Z0-9_-]{1,20}$"
End With
code that uses it:
Set vbRegXMatch = vbRegX.Execute(Me.txtProduct.Text)
If vbRegXMatch.Count = 1 Then
MsgBox "This string has invalid characters in it. Illegal characters are out side of the following ranges:" & vbNewLine & vbNewling & "a-z or A-Z" & vbNewLine & vbNewling & "0-9, - or _. Please try again."
Cancel = True
Me.txtProduct.SetFocus
Set vbRegXMatch = Nothing
Set vbRegX = Nothing
Exit Sub
End If
This code fires with invalid characters but not when length is > 20. This is the output given to me by Regex Buddy:
Dim FoundMatch As Boolean
Dim myRegExp As RegExp
Set myRegExp = New RegExp
myRegExp.Pattern = "^[a-zA-Z0-9_-]{1,20}$"
FoundMatch = myRegExp.Test(SubjectString)
Can anyone so kindly point out what Im missing?
visual of the control:
Your regex matches valid input. Thus, you need to .Test(your_string) and if the result is False, you need to fire an error.
Replace
Set vbRegXMatch = vbRegX.Execute(Me.txtProduct.Text)
If vbRegXMatch.Count = 1 Then
with
If vbRegX.Test("1234555") = False Then
Also, since you expect a single match, use
.Global = False

Clean blanks/whitespace to vbNull with RegEx

I am looking to clean up a .csv file for a database import. I am using the following vbs function and would like to incorporate '' to vbNull. I find it hard to understand RegEx. Can this even be done?
Function removeEmbeddedCommasInCSVTextField (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = """[^""]*,[^""]*"""
Set objMatch = objRegExp.Execute( strtoclean )
corrected_row = strtoclean
For Each myMatch in objMatch
matched_value = myMatch.Value ' retrieves text column with embedded commas
cleaned_value = replace(matched_value, ",","") ' removes embeddes commans from column
corrected_row = replace(corrected_row, matched_value, cleaned_value) 'take row and replaced bad value with good value (no commas)
Next
removeEmbeddedCommasInCSVTextField = corrected_row
End Function
MAIN:
Set MyFile = fso.CreateTextFile(strShareDirectory & "fixed.txt", True)
Set f = fso.OpenTextFile(strShareDirectory & filename)
Do Until f.AtEndOfStream
before_clean = f.ReadLine
after_clean = removeEmbeddedCommasInCSVTextField(before_clean)
MyFile.WriteLine(after_clean)
'WScript.Echo after_clean
Loop
f.Close
MyFile.Close

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:
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":
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

Asp regexp result listing

strDeger1 = """ >"
strDeger2 = "</a>"
strBaslikBul = VeriBul(strDeger1, strDeger2)
Response.Write strBaslikBul
Response.Write "<hr>"
Set RegEx = New RegExp
RegEx.IgnoreCase = True
RegEx.Global = True
RegEx.Pattern = "(" & BaslangicDegeri & ")(.+?)(" & BitisDegeri & ")"
Set Sonuc = RegEx.Execute(Trim(tr(Mid(HTTPObj2.GetURL,29600,254000))))
For Each i In Sonuc
VeriBul = i.SubMatches(1)
Next
If IsEmpty(VeriBul) Then VeriBul = "Sonuc Yok"
Set RegEx = Nothing
Set Sonuc = Nothing
Set objXmlHttp = Nothing
End Function
I have like this code, I'm trying to make listing all for records. But just one of record viewing.
How I can fix this?
Thanks.
Also need to concat for each match. Modify line
VeriBul = i.SubMatches(1) with VeriBul = VeriBul & i.SubMatches(1)