Regex excluding all numbers - regex

Hello I'm trying to search all the matching expressions in a file through a Regex in VB.NET
I have the function:
Dim written As MatchCollection = Regex.Matches(ToTreat, "\bGlobalIndexImage = \'(?![0-9])([A-Za-z])\w+\'")
For Each writ As Match In written
For Each w As Capture In writ.Captures
MsgBox(w.Value.ToString)
Next
Next
I have this Regex now:
\bGlobalIndexImage = \'(?![0-9])([A-Za-z])\w+\'
I'm trying to match all occurrences under this form:
GlobalIndexImage = 'images'
GlobalIndexImage = 'Search'
But I also get values like this which I don't want to match:
GlobalIndexImage = 'Z0003_S16G2'
So I wanted in my Regex to simply exclude a match if it contains numbers.

The \w shorthand character class matches letters and digits and _. If you need only letters, just use [a-zA-Z]:
"\bGlobalIndexImage = '([A-Za-z]+)'"
See the regex demo.
Details:
\b - a leading word boundary
GlobalIndexImage = ' - a string of literal chars
([A-Za-z]+) - Group 1 capturing one or more (due to + quantifier) ASCII letters
' - a single quote.
If you need to match any Unicode letters, replace [a-zA-Z] with \p{L}.
VB.NET:
Dim text = "GlobalIndexImage = 'images' GlobalIndexImage = 'Search'"
Dim pattern As String = "\bGlobalIndexImage = '([A-Za-z]+)'"
Dim matches As List(Of String) = Regex.Matches(text, pattern) _
.Cast(Of Match)() _
.Select(Function(m) m.Groups(1).Value) _
.ToList()
Console.WriteLine(String.Join(vbLf, matches))
Output:

To catch everything that's not a number use \D
So your regex will be something like
\bGlobalIndexImage = \'\d+\'
But this will also include words with white spaces. To get only letters use [a-zA-Z]
\bGlobalIndexImage = \'[a-zA-Z]+\'

Related

How to remove Emoji from string using VB

I need to remove some emoji characters from a string using classic asp and vb script. Here is what I have:
👪 Repeat / Other
📅 Scheduled
💲 Lead
And what I need:
Repeat / Other
Scheduled
Lead
I have been able to remove the emojis using this function but I want to keep special characters such as the forward slash /, spaces, &, :, etc.
Any help is appreciated.
Function strClean (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "((?![a-zA-Z0-9]).)+"
outputStr = objRegExp.Replace(strtoclean, "-")
objRegExp.Pattern = "\-+"
outputStr = objRegExp.Replace(outputStr, "")
strClean = outputStr
End Function
Your current regex matches any char but a line break and ASCII alphanumeric chars. It does not match emojis because VBScript ECMA-262 3rd edition based regex engine cannot match astral plane chars with a mere . pattern.
If you want to just add the emoji matching support to your current pattern, you can replace the . with (?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]) pattern and use
objRegExp.Pattern = "(?:(?![a-zA-Z0-9])(?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))+"
See the regex demo
If you just want to remove all but ASCII chars, you can use
objRegExp.Pattern = "objRegExp.Pattern = "(?:(?![ -~])[\s\S])+"
The pattern matches any one or more (+) chars ([\s\S] matches any whitespace and non-whitespace chars) that does not equal the printable ASCII chars.

how to extract numbers from string in dart language [duplicate]

How do I say remove a number preceded by a non-digit and followed by a dash, but leave the preceding non-digit character?
RegExp: /[^\D]4\-/
String: http://localhost/images/4-6-.png
Remove: 4-
The 4- should be removed and it should leave the preceding / or -
This would work: /4\-/
But it would also remove 14- or 44-
Dynamic Code:
http://jsfiddle.net/flackend/8s9X9/2/
Static Code:
var category_id = 4;
var src = 'http://localhost/images/4-6-.png';
var regexp = new RegExp('[^\\D]'+ category_id +'\\-')
$('p').append('regexp: '+ regexp +'<br>');
$('p').append(src +'<br>');
src = src.replace(regexp, '');
$('p').append(src);
You want [\D] or [^\d], but not [^\D]. Regex is case-sensitive, \d matches a digit, and \D matches anything but a digit.

How can I replace all spaces into a dash in a captured subexpression of regex in VB.NET?

Here is my code:
regExp = New Regex("\[def\](.+?)\[\/def\]")
strTextToReplace = regExp.Replace(strTextToReplace, "$1")
I want to replace all spaces with a dash in "$1". How can I do that? Thanks.
You may use a match evaluator:
Dim rx = New Regex("(?s)\[def](.+?)\[/def]")
Dim result = rx.Replace(s, New MatchEvaluator(Function(m As Match)
Return String.Format("{0}", m.Groups(1).Value.Replace(" ", "-"))
End Function))
m is the Match object that is passed to the Regex.Replace method when a match occurs, and you replace all spaces with hyphens only in .Groups(1) (the first capturing group.
If you need to replace any whitespace with -, replace m.Groups(1).Value.Replace(" ", "-") with Regex.Replace(m.Groups(1).Value, "\s", "-").

How to match the particular part from the nth index of the specific character?

I have the input data as,
"Thumbnail":"/images/7.0.2.5076_1/spacer.gif","URL":"http://id800/home/LayoutManager/l1.html/1407462681_292_2_2_1398567201/"
And I want to match the l1.html part of it. It can be anything. So I want to match the Part of URL which occurs before the second last occurrence of the / and after the third last occurrence of the /. That part either the number, alphanumeric, or the alphnumeric with .html extension. so besically I want to match the part between the 3rd and 2nd / from end. I tried lots of combinations but I was unable to come up with. Any help would be great.
Pattern:
\".+?(\w+\.\w{3,5})\/.+?\"
\" will match starting and ending quote
.+? will match any number of characters
\w+ will match any number of words
\. will match .(dot)
\w{3,5} will match any word which are 3-5 characters long
\/ will match /(forward slash)
() these parenthesis capture in separate group
Code in action:
string pattern = "\".+?(\\w+\\.\\w{3,5})\\/.+?\"";
string text = "\"Thumbnail\":\"/images/7.0.2.5076_1/spacer.gif\",\"URL\":\"http://id800/home/LayoutManager/l1.html/1407462681_292_2_2_1398567201/\"";
MatchCollection matches = Regex.Matches(text, pattern);
if (matches != null && matches[0].Groups != null)
{
string value = matches[0].Groups[1].Value; //Output: l1.html
}
You have not provided the whole JSON string, but I think my snippet will help you get what you want anyway without regex. Add a reference to System.Web.Extensions, and use the following code:
Dim s As String = "[{""Thumbnail"":""/images/7.0.2.5076_1/spacer.gif"",""URL"":""http://id800/home/LayoutManager/l1.html/1407462681_292_2_2_1398567201/""}]" ' "[{""application_id"":""1"",""application_package"":""abc""},{""application_id"":""2"",""application_package"":""xyz""}]"
Dim jss As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim dict = jss.Deserialize(Of List(Of Object))(s)
For Each d In dict
For Each v In d
If v.Key = "URL" Then
Dim tmp = v.Value.Trim("/"c).ToString().Split("/"c)
MsgBox(tmp(tmp.Length - 2))
End If
Next
Next
Result:
The substring you need can be obtained without a regex by mere splitting the value with /, and accessing the last but one element.

Regular expression in vb.net

how to check particular value start with string or digit. here i attached my code. am getting error to like idendifier expected.
code
----
Dim i As String
dim ReturnValue as boolean
i = 400087
Dim s_str As String = i.Substring(0, 1)
Dim regex As Regex = New Regex([(a - z)(A-Z)])
ReturnValue = Regex.IsMatch(s_str, Regex)
error
regx is type and cant be used as an expression
Your variable is regex, Regex is the type of the variable.
So it is:
ReturnValue = Regex.IsMatch(s_str, regex)
But your regex is also flawed. [(a - z)(A-Z)] is creating a character class that does exactly match the characters ()-az, the range A-Z and a space and nothing else.
It looks to me as if you want to match letters. For that just use \p{L} that is a Unicode property that would match any character that is a letter in any language.
Dim regex As Regex = New Regex("[\p{L}\d]")
maybe you mean
Dim _regex As Regex = New Regex("[(a-z)(A-Z)]")
Dim regex As Regex = New Regex([(a - z)(A-Z)])
ReturnValue = Regex.IsMatch(s_str, Regex)
Note case difference, use regex.IsMatch. You also need to quote the regex string: "[(a - z)(A-Z)]".
Finally, that regex doesn't make sense, you are matching any letter or opening/closing parenthesis anywhere in the string.
To match at the start of the string you need to include the start anchor ^, something like: ^[a-zA-Z] matches any ASCII letter at the start of the string.
Check if a string starts with a letter or digit:
ReturnValue = Regex.IsMatch(s_str,"^[a-zA-Z0-9]+")
Regex Explanation:
^ # Matches start of string
[a-zA-Z0-9] # Followed by any letter or number
+ # at least one letter of number
See it in action here.