Combining two regular expression - regex

I have these two regular expression ,tried combining .But not working
Dim regExCheckLength As Regex = New Regex("^\w{10}$")
Dim regexCheckFormat As Regex = New Regex("\b(SSN|TC|EMP)")
I am new to reg ex,is there a way to combine

Use logical OR operator to combine both regexes.
Dim regExCheckLength As Regex = New Regex("^\w{10}$|\b(SSN|TC|EMP)")
To satisfy both, you need to use a positive lookahead like below,
Dim regExCheckLength As Regex = New Regex("^(?=\w{10}$).*\b(SSN|TC|EMP).*")

Related

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

Regular expression for extracting Classic ASP include file names

I am searching for a Regular Expression that can help me extract filename.asp from the below string. It seems like a simple task, but I am unable to find a solution.
This is my input:
<!-- #include file="filename.asp" -->
I want output of regular expression like this:
filename.asp
I did some research and find the following solution.
Regular Expression:
/#include\W+file="([^"]+)"/g
Example code (VB.NET):
Dim list As New List(Of String)
Dim regex = New System.Text.RegularExpressions.Regex("#include\W+file=""([^""]+)""")
Dim matchResult = regex.Match(filetext)
While matchResult.Success
list.Add(matchResult.Groups(1).Value)
matchResult = matchResult.NextMatch()
End While
Example code (C#):
var list = new List<string>();
var regex = new Regex("#include\\W+file=\"([^\"]+)\"");
var matchResult = regex.Match(fileContent);
while (matchResult.Success) {
list.Add(matchResult.Groups[1].Value);
matchResult = matchResult.NextMatch();
}
Improved Regular Expression (ignores spaces):
#include\W+file[\s]*=[\s]*"([^"]+)"

VBA Regular Expressions - Run-Time Error 91 when trying to replace characters in string

I am doing this task as part of a larger sub in order to massively reduce the workload for a different team.
I am trying to read in a string and use Regular Expressions to replace one-to-many spaces with a single space (or another character). At the moment I am using a local string, however in the main sub this data will come from an external .txt file. The number of spaces between elements in this .txt can vary depeneding on the row.
I am using the below code, and replacing the spaces with a dash. I have tried different variations and different logic on the below code, but always get "Run-time error '91': Object Variable or with clock variable not set" on line "c = re.Replace(s, replacement)"
After using breakpoints, I have found out that my RegularExpression (re) is empty, but I can't quite figure out how to progress from here. How do I replace my spaces with dashes? I have been at this problem for hours and spent most of that time on Google to see if someone has had a similar issue.
Sub testWC()
Dim s As String
Dim c As String
Dim re As RegExp
s = "hello World"
Dim pattern As String
pattern = "\s+"
Dim replacement As String
replacement = "-"
c = re.Replace(s, replacement)
Debug.Print (c)
End Sub
Extra information: Using Excel 2010. Have successfully linked all my references (Microsoft VBScript Regular Expressions 5.5". I was sucessfully able to replace the spaces using the vanilla "Replace" function, however as the number of spaces between elements vary I am unable to use that to solve my issue.
Ed: My .txt file is not fixed either, there are a number of rows that are different lengths so I am unable to use the MID function in excel to dissect the string either
Please help
Thanks,
J.H.
You're not setting up the RegExp object correctly.
Dim pattern As String
pattern = "\s+" ' pattern is just a local string, not bound to the RegExp object!
You need to do this:
Dim re As RegExp
Set re = New RegExp
re.Pattern = "\s+" ' Now the pattern is bound to the RegExp object
re.Global = True ' Assuming you want to replace *all* matches
s = "hello World"
Dim replacement As String
replacement = "-"
c = re.Replace(s, replacement)
Try setting the pattern inside your Regex object. Right now, re is just a regex with no real pattern assigned to it. Try adding in re.Pattern = pattern after you initialize your pattern string.
You initialized the pattern but didn't actually hook it into the Regex. When you ended up calling replace it didn't know what it was looking for pattern wise, and threw the error.
Try also setting the re as a New RegExp.
Sub testWC()
Dim s As String
Dim c As String
Dim re As RegExp
Set re = New RegExp
s = "hello World"
Dim pattern As String
pattern = "\s+"
re.Pattern = pattern
Dim replacement As String
replacement = "-"
c = re.Replace(s, replacement)
Debug.Print (c)
End Sub

Replace all but last instance of specified character

If I have a string like
10,000kg crane,21
how should I strip all commas but the last to get
10000kg crane,21
I'm thinking this is a regular expression problem.
It can be done with regular expressions by using a lookahead assertion. You want to replace the commas that have at least one comma after them. The only comma for which this lookahead will fail is the last one.
Try this:
s = Regex.Replace(s, ",(?=.*?,)", "")
See it working online: ideone
Another approach, which may perform much faster than a RegEx solution:
Dim s As String = "10,000kg crane,21"
Dim result As String = New StringBuilder(s).Replace(",", String.Empty, 0,
s.LastIndexOf(","c)).ToString()
The gist is that it will replace all occurrences of "," with an empty string between the first character and the index of the last ",".
I did some benchmarks running this and the proposed RegEx solution 1,000,000 times each; on my laptop, without compiling the RegEx, this solution is about seven (7) times faster. If you do compile the RegEx, it's still about twice as fast.
A none regex approach:
Dim text = "10,000kg crane,21"
Dim parts = text.Split(","c).Reverse
Dim result = String.Join("", parts.Skip(1).Reverse) & "," & parts.First
An uglier, yet valid alternative approach:
Dim strResult As String = Replace(Mid(strTarget, 1, strTarget.LastIndexOf(",")), ",", String.Empty) & _
Microsoft.VisualBasic.Right(strTarget, Len(strTarget) - strTarget.LastIndexOf(","))

How to generate strings by regexp in Scala

Suppose there is a simple regular expression (only | and * are allowed). How would you create a stream of strings of any size (from smaller to larger) which match this regular expression in Scala?
Use Xeger. Even though it's Java and not Scala, you should have no problem using it with Scala.
In Java:
String regex = "[ab]{4,6}c";
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);