Regex doesn't Work , Regex between 2 String - regex

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

Related

basic4android - regex match with url

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.

VB.Net regex random string

I have regex code that gets string between 2 strings from TextBox1.
TextBox1 looks something like this:
href="www.example.com/account/05798/john123">
href="www.example.com/account/4970/max16">
href="www.example.com/account/96577/killer007">
href="www.example.com/account/3077/hackerboy1337">
href="www.example.com/account/43210/king42">
So, it will get value from href="www.example.com/account/4321/ to "> (usernames)
The problem is, how to do it? My regex code:
(?<="href=""www.example.com/account/RANDOM_STRING/")(.*?)(?="">)
I know i could replace RANDOM_STRING with \w{4}, but some IDs are 5-digit.
You need a negated character class [^/] that matches any char but a /. So, replace RANDOM_STRING with [^/]*.
Also, in a regex pattern, to match ., you need to escape the dot - \..
Thus, your regex pattern can be fixed as
(?<="href=""www\.example\.com/account/[^/]*/").*?(?="">)
However, you may user a simpler regex with a capturing group:
"href=""www\.example\.com/account/[^/]*/"(.*?)"">
The value you need is in Match.Groups(1).Value.
Or another option would be to do this
Dim strOne As String = "www.example.com/account/43210/king42"
Dim strMain As String = Split(strOne, "/account/")(1)
Dim strSubOne As String = Split(strMain, "/")(0)
Dim strSubTwo As String = Split(strMain, "/")(1)

regex .NET to find and replace underscores only if found between > and <

I have a list of strings looking like this:
Title_in_Title_by_-_Mr._John_Doe
and I need to replace the _ with a SPACE from the text between the html"> and </a> ONLY.
so that the result to look like this:
Title in Title by - Mr. John Doe
I've tried to do it in 2 steps:
first isolate that part only with .*html">(.*)<\/a.* & ^.*>(.*)<.* & .*>.*<.* or ^.*>.*<.*
and then do the replace but the return is always unchanged and now I'm stuck.
Any help to accomplish this is much appreciated
How I would do it is to .split it and then .replace it, no need for regex.
Dim line as string = "Title_in_Title_by_-_Mr._John_Doe"
Dim split as string() = line.split(">"c)
Dim correctString as String = split(1).replace("_"c," "c)
Boom done
here is the string.replace article
Though if you had to use regex, this would probably be a better way of doing it
Dim inputString = "Title_in_Title_by_-_Mr._John_Doe"
Dim reg As New Regex("(?<=\>).*?(?=\<)")
Dim correctString = reg.match(inputString).value.replace("_"c, " "c)
Dim line as string = "Title_and_Title_by_-_Mr._John_Doe"
line = Regex.Replace(line, "(?<=\.html"">)[^<>]+(?=</a>)", _
Function (m) m.Value.Replace("_", " "))
This uses a regex with lookarounds to isolate the title, and a MatchEvaluator delegate in the form of a lambda expression to replace the underscores in the title, then it plugs the result back into the string.

Regex pattern to match period and pattern

I have a string which I am trying to write an regex for
CODAA0870E - This an error string is not valid.
I wrote a regex COD[a-zA-Z0-9]*.....................................
but the length of the string can vary i.e. after COD till the period.
The regex needs to check COD at the start and should end at the period.`
The code I have written so far does not work
Dim value As String = "daafasfasfCODAA0870E - This an error string is not valid.dfsfsfsfcCODAAvcv0870E - This an second error string is not valid.sdfsdf "
Dim pattern As String = "COD[^.]+\."
Dim array() As String = System.Text.RegularExpressions.Regex.Split(value, pattern)
You need this regex:
Dim pattern As String = "COD[^.]+\."
And to get all matches use:
Dim matches As MatchCollection = Regex.Matches(value, pattern)
See more code samples here
Think you want something like this,
^COD[^.]*\.

Regular Expression for last folder in path

I've been trying to capture the last folder in a folder path using regular expressions in C# but am just too new to this to figure this out. For example if I have C:\Projects\Test then the expression should return Test. If I have H:\Programs\Somefolder\Someotherfolder\Final then the result should be Final. I've tried the below code but it just blows up. Thanks for any help.
string pattern = ".*\\([^\\]+$)";
Match match = Regex.Match("H:\\Projects\\Final", pattern, RegexOptions.IgnoreCase);
Why are you using a regex. You can just use DirectoryInfo.Name
var directoryname = new DirectoryInfo(#"C:\Projects\Test").Name;
\\The variable directoryname will be Test
this is a bad use of regular expressions when you have a pretty complete set of .NET libraries that can do this for you... two easy methods using System.IO.Path or System.IO.DirectoryInfo below
string path = #"H:\Programs\Somefolder\Someotherfolder\Final";
Console.WriteLine(System.IO.Path.GetFileName(path));
Console.WriteLine(new System.IO.DirectoryInfo(path).Name);
Perhaps this?
string strRegex = #".*\\(.*)"; RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"H:\Programs\Somefolder\Someotherfolder\Final";
string strReplace = #"$1";
return myRegex.Replace(strTargetString, strReplace);
Why don't use split?
string str = "c:\temp\temp1\temp2" ;
string lastfolder = str.Split("\").Last ;