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

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.

Related

Regex to replace all non numbers but allow a '+' prefix

I want to delete all invalid letters from a string which should represent a phone number. Only a '+' prefix and numbers are allowed.
I tried in Kotlin with
"+1234abc567+".replace("[^+0-9]".toRegex(), "")
It works nearly perfect, but it does not replace the last '+'.
How can I modify the regex to only allow the first '+'?
You could do a regex replacement on the following pattern:
(?<=.)\+|[^0-9+]+
Sample script:
String input = "+1234abc567+";
String output = input.replaceAll("(?<=.)\\+|[^0-9+]+", "");
System.out.println(input); // +1234abc567+
System.out.println(output); // +1234567
Here is an explanation of the regex pattern:
(?<=.)\+ match a literal + which is NOT first (i.e. preceded by >= 1 character)
| OR
[^0-9+]+ match one or more non digit characters, excluding +
You can use
^(\+)|\D+
Replace with the backreference to the first group, $1. See the regex demo.
Details:
^(\+) - a + at the start of string captured into Group 1
| - or
\D+ - one or more non-digit chars.
NOTE: a raw string literal delimited with """ allows the use of a single backslash to form regex escapes, such as \D, \d, etc. Using this type of string literals greatly simplifies regex definitions inside code.
See the Kotlin demo:
val s = "+1234abc567+"
val regex = """^(\+)|\D+""".toRegex()
println(s.replace(regex, "$1"))
// => +1234567

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.

Regex excluding all numbers

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]+\'

Regex update in C#

This regex pattern is not finding PageName correctly when pagename contains - like PAGE-NAME=HomePage-Left-Of-Calendar but it works when PAGE-NAME=HomePage can anyone help me to update this regex to support both...
var result = " <div id=\"header-left\" style=\"border:none\">";
result += "<!-- CMS-PAGE PAGE-NAME=HomePage-Left-Of-Calendar -->";
result += " </div>";
var pattern = #"(<!--)(\s*)(CMS-PAGE)(\s*)((PAGE-NAME)(=)(?<value>\w*)(\s*))*(\s*)(-->)";
var replaceTag = " <widget:ContentPageView runat=\"server\" PageName=\"##value\" Editable=\"True\">" +
"<DescriptionTemplate>...</DescriptionTemplate>" +
"</widget:ContentPageView>";
result = RegexReplaceWithUniqueTag(result, pattern, replaceTag);
The issue is a specific part of your regular expression:
(?<value>\w*)
This is matching only word characters (that's what \w means); however, you're also asking it to match non-word characters (specifically, the hyphen). Change this to:
(?<value>(\w|-)*)
and you will match both HomePage and HomePage-Left-Of-Calendar.
You need to add the hyphen (-) in ordered to be matched.
You can simply place both \w and - inside of a character class [] together which will match any character of word characters (a-z, A-Z, 0-9, _) and the literal character -
(?<value>[\w-]*)

Regular Expression remove leading blank and dash character

Given a string like String a="- = - - What is your name?";
How to remove the leading equal, dash, space characters, to get the clean text,
"What is your name?"
If you want to remove the leading non-alphabets you can match:
^[^a-zA-Z]+
and replace it with '' (empty string).
Explanation:
first ^ - Anchor to match at the
begining.
[] - char class
second ^ - negation in a char class
+ - One or more of the previous match
So the regex matches one or more of any non-alphabets that are at the beginning of the string.
In your case case it will get rid of all the leading spaces, leading hyphens and leading equals sign. In short everything before the first alphabet.
$a=~s/- = - - //;
In Javascript you could do it like this
var a = "- = - - What is your name?";
a = a.replace(/^([-=\s]*)([a-zA-Z0-9])/gm,"$2");
Java:
String replaced = a.replaceFirst("^[-= ]*", "");
Assuming Java try this regex:
/^\W*(.*)$/
retrieve your string from captured group 1!
\W* matches all preceding non-word characters
(.*)then matches all characters to the end beginning with the first word character
^,$ are the boundaries. you could even do without $ in this case.
Tip try the excellent Java regex tutorial for reference.
In Python:
>>> "- = - - What is your name?".lstrip("-= ")
'What is your name?'
To remove any kind of whitespace, use .lstrip("-= \t\r\n").
In Javascript, I needed to do this and did it using the following regex:
^[\s\-]+
and replace it with '' (empty string) like this:
yourStringValue.replace(/^[\s\-]+/, '');