I have some forms in which users will input some numbers, I want to prevent them from entering more than one comma in this string
I made something like this
var input = "1,,,,2";
var value = Regex.Replace(input, ",{1,}", ".");
This will output 1.2, which is correct. But if I enter
var input = 1,,,2,3,,,4,5,,6
everything fails
What id like to do is to form the last version of the input to 1.23456
Any advice?
Thanks
Regex.Replace (input, #"(?<=^\d+),", ".").Replace (",", "");
This replaces the first , comma with a . period, then replaces the remaining commas with empty.
Use this regex: (?<!,[^,]*?),+
var res = Regex.Replace(input, #"(?<!,[^,]*?),+", ".").Replace(",", string.Empty);
Or this code:
var res = Regex.Replace(input, #"(?<!,[^,]*?)(,+)|(,+)",
m => m.Groups[1].Success ? "." : string.Empty);
Output: 1.23456
Related
I have a string with 2 possibilities:
var desc = "Keyword1: That text I want \r\n Keyword2: Value2 \r\n Keyword3: Value3 \r\n Keyword4: Value4"
var desc = "Keyword1: That text I want Keyword2: Value2 \r\n Keyword3: Value3 \r\n Keyword4: Value4"
where the order of the keywords after the text "That text I want" Keyword2, Keyword3, Keyword4 doesn't matter and they are all optional.
I tried with the Regex Keyword1:(\s+)(.*)(\W+?)(\r\n?)(?=Keyword2:|Keyword3:|Keyword4:)
It does not work. Not sure what is wrong in my regex.
Any help is highly appreciated.
Thanks in advance!
Show here for the solution.
In your case you could simply use (regex between two strings):
(?<=Keyword1:)(.*)(?=Keyword2)
Try it out
Hope it helps.
Assuming those \r\n are actual special characters in the string and not the literals, this should work:
Keyword1: (.*?)(Keyword2:|Keyword3:|Keyword4:|\r\n)
You need to get the second grouping from the match. For example: match.Groups[1].
This regex matches Keyword1:, followed by the minimum amount of necessary characters, and then followed by either Keyword2: or \r\n (special characters). If those are literals in your input string, you will need to double those backslashes.
You can check it here. Note that on the right, Group 1 contains your text in both cases.
var pattern = keywordName + #":\s+(.+?)\r?\n";
var regex = new Regex(pattern);
var match = regex.Match(description);
if (!match.Success) return null;
var firstMatch = match.Groups[1].Value;
//Find if there's another keyword in the extracted Value
var lstKeywords = Enum.GetValues(typeof(Keywords)).Cast<Keywords>().Where(k => k != keywordName);
//Add : to the last value so that it's recognized as a keyword
var sOtherKeywords = string.Join(":|", lstKeywords) + ":";
var pattern2 = #"(" + sOtherKeywords + #")(\s+)";
regex = new Regex(pattern2);
match = regex.Match(firstMatch);
//If there's no other keyword in the same line then return the expression that is extracted from the first regex
if (!match.Success) return firstMatch;
var secondMatch = match.Groups[1].Value;
var pattern3 = keywordName + #":\s+(.+)(\r?\n?)" + secondMatch;
regex = new Regex(pattern3);
match = regex.Match(description);
return match.Success ? match.Groups[1].Value.TrimEnd() : null;
I want to match only numbers in the following string
String : "40’000"
Match : "40000"
basically tring to ignore apostrophe.
I am using C#, in case it matters.
Cant use any C# methods, need to only use Regex.
Replace like this it replace all char excpet numbers
string input = "40’000";
string result = Regex.Replace(input, #"[^\d]", "");
Since you said; I just want to pick up numbers only, how about without regex?
var s = "40’000";
var result = new string(s.Where(char.IsDigit).ToArray());
Console.WriteLine(result); // 40000
I suggest use regex to find the special characters not the digits, and then replace by ''.
So a simple (?=\S)\D should be enough, the (?=\S) is to ignore the whitespace at the end of number.
DEMO
Replace like this it replace all char excpet numbers and points
string input = "40’000";
string result = Regex.Replace(input, #"[^\d^.]", "");
Don't complicate your life, use Regex.Replace
string s = "40'000";
string replaced = Regex.Replace(s, #"\D", "");
I have the following string that I will need to remove everything between =select and the following } char
ex.
Enter Type:=select top 10 type from cable}
The end result is the string variable to just show Enter Type:
I was looking for a way to do this with Regex, but I'm open to other methods as well. Thanks in advance for the help.
string input = "Enter Type:=select top 10 type from cable}";
System.Text.RegularExpressions.Regex regExPattern = new System.Text.RegularExpressions.Regex("(.*):=select.*}");
System.Text.RegularExpressions.Match match = regExPattern.Match(input);
string output = String.Empty;
if( match.Success)
{
output = match.Groups[1].Value;
}
Console.WriteLine("Output = " + output);
The value of the 'output' variable will be the value found before the ":=select" segment of the input string. If you need to pull out additional information from the input string, surround it will parenthesis and matches found will be added to the match.Groups array. By the way, the value of match.Groups[0].Value is the original string.
var rx = new Regex("=select[^}]*}");;
Console.WriteLine(rx.Replace ("Enter Type:=select top 10 type from cable}", ""));
Regexp.Replace(string input,string output) function replaces all substrings that match given regexp with string "output". First line defines regexp that matches everything between =select and }
So, I got an regular expression :
(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])
That should found all letters and replace it with a blank.
var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])", " ");
But when I got for example :
45a, nomDoc become 45 a, while I juste want 45
Did I write this regex wrong? I'm not very good at it, but I was thinking I was good for this one.
The regex must replace all non-numeric characters, following a numeric character or all non-numeric char before numeric.
45a or a45 must give me 45.
Thank you.
What you're doing, is searching for a spot where the string changes from digits to letters or from letters to digits and insert a space there. So yes, 45a becomes 45 a.
If you want to replace all letters with a blank, use
var nomDoc = Regex.Replace(arr[0], "[A-Za-z]", " ");
But I doubt that this is what you want.
If you want to remove all letters, replace with an empty string instead of a space.
If you want to replace all letters following a digit with a space, use
var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");
Try this:
var str = "1 oo 23ksls 4910fsj2jd43ld fkkd ^&?&;#";
var nomDoc = str.Replace('/([^0-9]|\n)/g', ' ');
This replaces all the non-number characters(letters, whitespaces and characters) with a space.
You could try this :
var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");
If you are using Javascript, here's a fiddle :
var Str = "blablabla22445543__-_-_-_-_-_-èèpzofez5zsqef*f-e+ffnfuf'3";
var nomDoc = Str.replace(/[^0-9]/g, "");
$("#result").html(nomDoc);
http://jsfiddle.net/ZqF6L/
It's not quite clear if you want to replace all non-numeric characters with spaces or just remove then completely.
Depending on that, either
var nomDoc = Regex.Replace(arr[0], "[^0-9]", " ");
or
var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");
should do what you want.
hey if you want to remove all your words then use below format method
var demo= Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");
I have a function containing the following code:
Text = Text.Where(c => !Char.IsDigit(c)).Aggregate<char, string>(null, (current, c) => current + c);
but it is rather slow. Is there anyway I can speed it up?
Try this regex:
Text = Regex.Replace(Text, #"\d+", "");
\d+ is more efficient than just \d because it removes multiple consecutive digits at once.
Yes, you can use Regex.Replace:
Text = Regex.Replace(Text, "\\d", "");
The regular expression matches a single digit. Regex.Replace replaces each occurrence of it in the Text string with an empty string "".
All those concatenations are probably killing you. The easiest/best is probably a regex:
Text = Regex.Replace(Text, "\\d", "");
Or you can try making only one new string instance:
Text = new string(Text.Where(c => !Char.IsDigit(c)).ToArray())
Try with Regex.Replace;
In a specified input string, replaces strings that match a regular
expression pattern with a specified replacement string.
Regex.Replace(Text, "\\d+", "");
Here is a DEMO.