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)
Related
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
I'm currently having difficulty with a VBScript I'm writing that contains several read and replaces from a text file. The expression I'm using finds the expression and replaces it, but adds three tab spaces afterwords, making the original line below it mess up the formatting. Here's a picture of what I'm talking about:
Here's a pastebin of the before and after, rather than an image:
https://pastebin.com/Uw3H59QK
Here's my RegExp code:
Set fso = CreateObject("Scripting.FileSystemObject")
Dim strPath
strPath = SelectFolder( "" )
If strPath = vbNull Then
WScript.Echo "Script Cancelled - No files have been modified" 'if the user cancels the open folder dialog
Else
WScript.Echo "Selected Folder: """ & strPath & """" 'prompt that tells you the folder you selected
End If
Function SelectFolder( myStartFolder )
Dim objFolder, objItem, objShell
Dim objFolderItems
On Error Resume Next
SelectFolder = vbNull
Set objShell = CreateObject( "Shell.Application" )
Set objFolder = objShell.BrowseForFolder( 0, "Please select the .dat file location folder", 0, myStartFolder)
set objFolderItems = objFolder.Items
If IsObject( objFolder ) Then SelectFolder = objFolder.Self.Path
Set objFolder = Nothing
Set objShell = Nothing
On Error Goto 0
End Function
Set re = New RegExp 'Replacing Position Lines
re.Pattern = "Pos = \((.*)\)"
re.Global = True
re.IgnoreCase = True
For Each f in fso.GetFolder(strPath).Files
If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
text = f.OpenAsTextStream.ReadAll 'reading the text file
f.OpenAsTextStream(2).Write re.Replace(text, """Position"" : mathutils.Vector(($1)),")
count = count + 1
End If
Next
Set reAngles = New RegExp 'Replacing Angles
reAngles.Pattern = "Angles = \((.*)\)"
reAngles.Global = True
reAngles.IgnoreCase = True
For Each f in fso.GetFolder(strPath).files
If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
text = f.OpenAsTextStream.ReadAll
f.OpenAsTextStream(2).Write reAngles.Replace(text, """Angles"" : mathutils.Vector(($1)),")
End If
Next
Set reNames = New RegExp 'Replacing Names
reNames.Pattern = "Name = (.*)"
reNames.Global = True
'reNames.Multiline = True
reNames.IgnoreCase = True
For Each f in fso.GetFolder(strPath).files
If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
text = f.OpenAsTextStream.ReadAll
f.OpenAsTextStream(2).Write reNames.Replace(text, """Name"" : ""$1"",")
End If
Next
My best guess is that the wildcard is grabbing more info than needed...but I'm unsure how to fix that. I used a lot of these expressions in Notepad++ so I was hoping to translate them to a VBS easily!
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
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
How can i remove style attribute from any tag with regex in asp?
from:
<div style="margin-top:10px;">test</div>
to:
<div>test</div>
Set objRegExp = New regexp
objRegExp.Pattern = "/style\s*=\s*(\'|').+(\'|')/i"
objRegExp.IgnoreCase = True
objRegExp.Global = True
Set resp = objRegExp.Execute(strWordHTML)
For Each respItem In resp
strWordHTML= replace(strWordHTML,respItem.Value,"")
Next
Set resp = Nothing
Set objRegExp = Nothing
solved *
(\sstyle=['""][^'""]+?['""])
Not using regex and not tested but something like this should work
str = "<div style=""margin-top:10px;"">test</div>"
start = InStr(str, "style")
first = InStr(start, str, """")
second = InStr(first, str, """")
result = Mid(str, 1, start - 1) + Mid(str, second + 1)
dim result = Regex.Replace(HtmlText, "style[^>]*", "")