basic4android - regex match with url - regex

I have the following regex pattern for testing a url and it works fine with all online regex testers including b4a's original regex tester(https://b4x.com:51041/regex_ws/index.html) but not in code!!
Sub Validate(Url As String)As String
Dim Pattern As String=$"^(https:\/\/t\.me\/|https:\/\/telegram\.me\/)[a-z0-9_]{3,15}[a-z0-9]$"$
Dim matcher1 As Matcher
matcher1 = Regex.Matcher(Url,Pattern)
Return matcher1.Find
End Sub
And my Url is
Https:// telegram . me (+ something like 'myChannel' with no spaces ofcurse,its just stacks's editor that won't allow tg link so if u wanted to check remove the spaces)
always returns false at all forms

tnx to #bulbus the solution for anyone that may face this problem is:
Sub Validate(Url As String)As String
Dim Pattern As String=$"^(https:\/\/t\.me\/|https:\/\/telegram\.me\/)[a-zA-Z0-9_]{3,15}[a-zA-Z0-9]$"$
Dim matcher1 As Matcher
matcher1= Regex.Matcher2(Pattern,Regex.MULTILINE,Url)
Return matcher1.Find
End Sub

Option 1
Use
matcher1 = Regex.Matcher(Url,Pattern,RegexOptions.IgnoreCase)
OR
Option 2
Use
Dim Pattern As String=$"^(https:\/\/t\.me\/|https:\/\/telegram\.me\/)[a-zA-Z0-9_]{3,15}[a-zA-Z0-9]$"$
Instead of
Dim Pattern As String=$"^(https:\/\/t\.me\/|https:\/\/telegram\.me\/)[a-z0-9_]{3,15}[a-z0-9]$"$
I hope both solutions are self explanatory!
EDIT
After OP accepted the answer, just a little bit of explanation. The LineBegin ^ and LineEnd $ identifiers are recognised only in MULTILINE mode otherwise they are ignored.

Related

Replacing everything but the matching regex string

I've searched for this answer but haven't found an answer that exactly works.
I have the following pattern where the hashes are any digit: 102###-###:#####-### or 102###-###:#####-####
It must start with 102 and the last set in the pattern can either be 3 or 4 digits.
The problem is that I can have a string with between 1-5 of these patterns in it with any sort of characters in between (spaces, letters etc). The Regex I posted below matches the patterns well but I am trying to select everything that is NOT this pattern so I can remove it. The end goal is to extract all the patterns and just have all the patterns comma delimited as the output. (Pattern, Pattern, Pattern) How do I accomplish this with regex?Perhaps there is a better way than trying to take this line? Thanks. This is using VBA.
Regex For Pattern:(\D102\d{3}-\d{3}:\d{5}-\d{3,4}\D)
String Example: type:102456-345:56746-234 102456-345:56746-2343 FollowingCell#:102456-345:56746-234 exampletext##$% 102456-345:56746-2345 stuff
No need to grab everything you don't need to remove it: That's more difficult. Just grab everything you need and do whatever you want with it.
See regex in use here
(?<!\d)102\d{3}-\d{3}:\d{5}-\d{3,4}(?!\d)
See code in use here
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim sourcestring as String = "type:102456-345:56746-234 102456-345:56746-2343 FollowingCell#:102456-345:56746-234 exampletext##$% 102456-345:56746-2345 stuff"
Dim re As Regex = New Regex("(?<!\d)102\d{3}-\d{3}:\d{5}-\d{3,4}(?!\d)")
Dim mc as MatchCollection = re.Matches(sourcestring)
For each m as Match in mc
Console.WriteLine(m.Groups(0).Value)
Next
End Sub
End Module
Result:
102456-345:56746-234
102456-345:56746-2343
102456-345:56746-234
102456-345:56746-2345
I am trying to select everything that is NOT this pattern so I can remove it. The end goal is to extract all the patterns and just have all the patterns comma delimited as the output
If you want to extract the patterns, then just do that, without removing everything around them. Example in Python: (Posted before the question's language was specified, but I'm sure the same can be done in VBA.)
>>> import re
>>> p = r"102\d{3}-\d{3}:\d{5}-\d{3,4}"
>>> text = "type:102456-345:56746-234 102456-345:56746-2343 FollowingCell#:102456-345:56746-234 exampletext##$% 102456-345:56746-2345 stuff"
>>> ",".join(re.findall(p, text))
'102456-345:56746-234,102456-345:56746-2343,102456-345:56746-234,102456-345:56746-2345'

Match shortest option

I'm trying to use Outlook 2013 VBA to modify an email body by pulling out and replacing a < span> section. However, with multiple spans, I'm having trouble forcing the regex to only pick up one span.
Based on some other searches, I'm trying to use negative lookahead, but failing at it.
Result from below is: <span><span style = blah blah>Tags: test, test2</span>
Desired result is: <span style = blah blah>Tags: test, test2</span>
Code for test module:
Sub regextest()
Dim regex As New RegExp
Dim testStr As String
testStr = "a<span><span style=blah blah>Tags: test, test2</span></span>"
regex.pattern = "<span.*?(?:(span)).*?Tags:.*?</span>"
Set matches = regex.Execute(testStr)
For Each x In matches
Debug.Print x 'Result: <span><span style = blah blah>Tags: test, test2</span>
Next
End Sub
Thank you!
Wiktor's answer in comments above works for my purposes:
<span\b[^<]*>[^<]*Tags:[^<]*</span>
This works as long as there are no '<' between the two span ends. Not really a lookahead, but it's good enough for what I'm doing and very simple.
Thanks Wiktor!

Regex doesn't Work , Regex between 2 String

Please help me. I have a strange error with Regex:
Const AGC = "s$(.*)s"
Dim ORIGINAL As String = "s$1s"
Dim lel As Regex = New Regex(AGC)
Dim lol As Match = lel.Match(ORIGINAL)
MsgBox(lol.Success)
MsgBox(lol.Groups(0).Value)
The following code doesn't work , i don't know why.
This is because the $ is a special character. You need to escape it in your Regex:
Const AGC = "s\$(.*)s"
The rest of the code should work fine:
Const AGC = "s\$(.*)s"
Dim ORIGINAL As String = "s$1s"
Dim lel As Regex = New Regex(AGC)
Dim lol As Match = lel.Match(ORIGINAL)
MsgBox(lol.Success)
MsgBox(lol.Groups(0).Value)
See it in action here.
I posted a C# version of this answer in the previous revision accidentally!
$ is a special regex character showing end-of-line. You'll have to use \$ to actually specify the dollar character in the regex expression. Your new expression will be "s\$(.*)s". Use sites such as Regex Storm .Net tester to test your regexes when you're new to them. Link to this regex and its test:
http://regexstorm.net/tester?p=s%5c%24(.*)s&i=s%241s

How to use non-capturing group in VBA regex?

With VBA, I'm trying to use regex to capture the filename from a UNC path without the extension--looking at .TIF files only.
So far this is what I have:
Function findTIFname(filestr As String) As String
Dim re As RegExp
Dim output As String
Dim matches As MatchCollection
Set re = New RegExp
re.pattern = "[^\\]+(?:[.]tif)$"
Set matches = re.Execute(filestr)
If matches.Count > 0 Then
output = matches(0).Value
Else
output = ""
End If
findTIFname = output
End Function
But when I run the function as follows:
msgbox findTIFname("\\abc\def\ghi\jkl\41e07.tif")
I get the following output:
41e07.tif
I thought that "(?:xxx)" was the regex syntax for a non-capturing group; what am I doing wrong?
The syntax (?:...) is a non-capturing group. What you need here is a positive lookahead assertion which has a (?=...) syntax like so:
re.pattern = "[^\\]+(?=[.]tif$)"
Note that lookaround assertions have zero width and consume no characters.
Do you really need to do this with RegEx?
Access (or better, MS Office) has built-in ways to do this quite easily without RegEx.
You just need to reference the Microsoft Scripting Runtime (which should be included in every MS Office installation, as far as I know).
Then you can use the FileSystemObject:
Public Function findTIFname(filestr As String) As String
Dim fso As FileSystemObject
Set fso = New FileSystemObject
If fso.GetExtensionName(filestr) = "tif" Then
findTIFname = fso.GetBaseName(filestr)
End If
Set fso = Nothing
End Function
Given your example UNC path \\abc\def\ghi\jkl\41e07.tif, this will return 41e07.

using classic asp for regular expression

We have some Classic asp sites, and i'm working on them a lil' bit, and I was wondering how can I write a regular expression check, and extract the matched expression:
the expression I have is in the script's name
so Let's say this
Response.Write Request.ServerVariables("SCRIPT_NAME")
Prints out:
review_blabla.asp
review_foo.asp
review_bar.asp
How can I get the blabla, foo and bar from there?
Thanks.
Whilst Yots' answer is almost certainly correct, you can achieve the result you are looking for with a lot less code and somewhat more clearly:
'A handy function i keep lying around for RegEx matches'
Function RegExResults(strTarget, strPattern)
Set regEx = New RegExp
regEx.Pattern = strPattern
regEx.Global = true
Set RegExResults = regEx.Execute(strTarget)
Set regEx = Nothing
End Function
'Pass the original string and pattern into the function and get a collection object back'
Set arrResults = RegExResults(Request.ServerVariables("SCRIPT_NAME"), "review_(.*?)\.asp")
'In your pattern the answer is the first group, so all you need is'
For each result in arrResults
Response.Write(result.Submatches(0))
Next
Set arrResults = Nothing
Additionally, I have yet to find a better RegEx playground than Regexr, it's brilliant for trying out your regex patterns before diving into code.
You have to use the Submatches Collection from the Match Object to get your data out of the review_(.*?)\.asp Pattern
Function getScriptNamePart(scriptname)
dim RegEx : Set RegEx = New RegExp
dim result : result = ""
With RegEx
.Pattern = "review_(.*?)\.asp"
.IgnoreCase = True
.Global = True
End With
Dim Match, Submatch
dim Matches : Set Matches = RegEx.Execute(scriptname)
dim SubMatches
For Each Match in Matches
For Each Submatch in Match.SubMatches
result = Submatch
Exit For
Next
Exit For
Next
Set Matches = Nothing
Set SubMatches = Nothing
Set Match = Nothing
Set RegEx = Nothing
getScriptNamePart = result
End Function
You can do
review_(.*?)\.asp
See it here on Regexr
You will then find your result in capture group 1.
You can use RegExp object to do so.
Your code gonna be like this:
Set RegularExpressionObject = New RegExp
RegularExpressionObject.Pattern = "review_(.*)\.asp"
matches = RegularExpressionObject.Execute("review_blabla.asp")
Sorry, I can't test code below right now.
Check out usage at MSDN http://msdn.microsoft.com/en-us/library/ms974570.aspx