I have this particular string:
Administrationsomkostninger I -2.889 - r0.l l0
I would like to replace these characters:r,l and i with 1.
I use this expression:
([(t|r|l|i|)])
That gives me this string:
Adm1n1s11a11onsomkos1n1nge1 1 -2.889 - 10.1 10
Now i want to replace the all digits that contains a digit followed + a whitespace
so in this case only - 10.1 10 gets converted to -10.110
Try this
string input = "Administrationsomkostninger I -2.889 - r0.l l0";
string pattern = #"(?'spaces'\s){2,}";
string output = Regex.Replace(input, pattern, " ");
Related
I am trying to convert a Pascal Case string with numbers to a sentence:
OpenHouse2StartTimestamp = > Open House 2 Start Timestamp
I've been able to use regex to separate them without numbers, thanks to this answer, but how to do so when numbers are present is eluding me:
string sentence = Regex.Replace(label, "[a-z][A-Z]", m => m.Value[0] + " " + m.Value[1]);
How can I add numbers into the mix?
You can use
var sentence = Regex.Replace(label, #"(?<=[a-z])(?=[A-Z])|(?<=\d)(?=\D)|(?<=\D)(?=\d)", " ");
See the .NET regex demo. The regex matches:
(?<=[a-z])(?=[A-Z])| - a location between a lower- and an uppercase ASCII letters, or
(?<=\d)(?=\D)| - a location between a digit and a non-digit, or
(?<=\D)(?=\d) - a location between a non-digit and a digit.
Since all you need is inserting a space at the positions matched, you do not need a Match evaluator, just use a string replacement pattern.
I want to validate a string to see if it is at least 6 digits long and has at least 1 int.
string text = "I want to run 10 miles daily";
string pattern = #"(?=\.*\d).{6,}";
Match match = Regex.Match(text, pattern);
Console.WriteLine(match.Value);
Please explain me why I am getting the below output:
"10 miles daily"
The reason you get "10 miles daily" is because you specify a positive lookahead (?=\.*\d) which matches a literal dot zero or more times and then a digit.
That assertion succeeds at the position before the 1 where it matches zero times a dot and then a digit:
I want to run 10 miles daily
..............|
From that moment you match any character zero or more times which will match .{6,} which matches:
I want to run 10 miles daily
^^^^^^^^^^^^^^
You could update your regex to remove the backslash before the dot and use anchors to assert the start ^ and the end $ of the line:
^(?=.*\d).{6,}$
That would match
^ Assert begin of a line
(?=.*\d) Positive lookahead to assert what what followes contains a digit
.{6,} Match any character 6 more times
$ Assert the end of a line
I know this doesn't answer your question, but I assume you're looking for a working RegEx. Your statement was I want to validate a string to see if it is at least 6 digits long and has at least 1 int.. I assume you mean at least 6 characters long (including white space) and has at least 1 int.
This should do it (C#):
#"^(?=.*\d+)(?=.*[\w]).{6,}$";
RegEx Analyzer: UltraPico Expresso RegEx Tool
Test code and output (C#)
tatic void Main(string[] args)
{
string text = "abcdef";
Match match;
string pattern = #"^(?=.*\d+)(?=.*[\w]).{6,}$";
match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
Console.WriteLine("Text:'"+text + "'. Matched:" + match.Success + ". Value:" + match.Value);
text = "abcdefg";
match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
Console.WriteLine("Text:'" + text + "'. Matched:" + match.Success + ". Value:" + match.Value);
text = "abcde1";
match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
Console.WriteLine("Text:'" + text + "'. Matched:" + match.Success + ". Value:" + match.Value);
text = "abcd21";
match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
Console.WriteLine("Text:'" + text + "'. Matched:" + match.Success + ". Value:" + match.Value);
text = "abcd dog cat 21";
match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
Console.WriteLine("Text:'" + text + "'. Matched:" + match.Success + ". Value:" + match.Value);
Console.ReadKey();
}
\.*\d this means string ends with a digit.
(?=\.*\d) this take target digit in string
(?=\.*\d). this pattern means, in a string, everything after the digit found.
(?=\.*\d).{6,} every characters after digit and match must be at least 6 characters.
You need this regex : (?=.*\d).{6,}$. This return as a result at least 6 character and has at least one of them is digit
\.*\d => means a digit preceded by literal dots(`.`)
and * means repeating (0 ~ ) numbers of the character, dot(.).
Thus, as you can see, there is no character dot(.) in your input string. So regex engine try to match by evaluating *'s repeating number as zero, 0.
As a result, your regex may be interpreted as follows.
(?=\d).{6,}
Yes, this regex means one digit followed by more than 5 numbers of any characters.
But, {6,} means greedy search which searches possible maximum length string. On the other hand, lazymode( {6,}? in this case) searches possible minimum length string.
You can try this lazy mode regex and compare to the above greedy one's result.
(?=\d).{6,}?
I'm looking for a Regex to validate a rating (1-10) + optional text.
Rating is a decimal, with 1 point, which can use both dot or comma separator.
Followed by an optional space + string.
Valid
7
7,5
7.5
7,5 This is my string
7.5 Hello
Invalid
7,75
11
7This is my string
7.This is my string
10.5 string
I've got this for getting the decimal values, but I'm not sure how to get the optional text behind it.
^(10|\d)([\.\,]\d{1,1})?$
Judging by your examples, the space after the initial number is not optional. Thus, the pattern you may use is
^(?:10|[1-9](?:[.,][0-9])?)(?:\s.*)?$
or - since a partial match with Regex.IsMatch is enough to validate the string - replace (?:\s.*)?$ with a negative lookahead (?!\S) that will require a whitespace or end of string after the number:
^(?:10|[1-9](?:[.,][0-9])?)(?!\S)
^^^^^^^
See the regex demo
Details:
^ - start of a string
(?:10|[1-9](?:[.,][0-9])?) - either 10 or a digit from 1 to 9 followed with an optional sequence of a , or . and any single digit and then...
(?:\s.*)?$ - an optional sequence of any whitespace followed with any chars up to the end of string - OR -
(?!\S) - a negative lookahead that fails the match if there is no non-whitespace char immediately to the right of the current position.
C# test:
var strs = new List<string> { "7","7,5","7.5","7,5 This is my string","7.5 Hello","7,75","11","7This is my string","7.This is my string","10.5 string"};
var pattern = #"^(?:10|[1-9](?:[.,][0-9])?)(?:\s.*)?$";
foreach (var s in strs)
if (Regex.IsMatch(s, pattern))
Console.WriteLine("{0} is correct.", s);
else
Console.WriteLine("{0} is invalid.", s);
Output:
7 is correct.
7,5 is correct.
7.5 is correct.
7,5 This is my string is correct.
7.5 Hello is correct.
7,75 is invalid.
11 is invalid.
7This is my string is invalid.
7.This is my string is invalid.
10.5 string is invalid.
I want to validate that a string follows this format (using regex):
valid: 123456789 //9 digits
valid: 12-1234567 // 2 digits + dash + 7 digits
Here's an example, how I would use it:
var r = new Regex("^[1-9]\d?-\d{7}$");
Console.WriteLine(r.IsMatch("1-2-3"));
I have the regex for the format with dash, but can't figure how to include the non-dash format???
Regex regex = new Regex("^\\d{2}-?\\d{7}$");
This will accept the two formats you want: 2 digits then an optional dash and 7 numbers.
^ \d{9} | \d{2} - \d{7} $
Remove the spaces, they are there for readability.
I have the following sample string:
string s = Console.ReadLine();
s= {6} {7613023456148 } {7.040 } {56780} {Sample String}
How do I achieve the following with regex or something similar:
Remove all numbers in a line that start with 7 and are 13 digits long.
Remove all decimal numbers.
Output
s = {6} {56780} {Sample String}
You can replace the captured string use following regex with an empty string :
7\d{12}|\d\.\d+
But Note that if your numbers are within {} you need :
\b{\s*7\d{12}\s*}\b|\b{\s*\d+\.\d+\s*}\b
See demo https://regex101.com/r/dL1vF4/1
You are looking for this regex:
var s = "{6} {7613023456148 } {7.040 } {56780} {Sample String}";
s = Regex.Replace(s, #"\s*{(?:\s*7[0-9]{12}\s*|\d+\.\d+\s*)}", string.Empty);
Console.WriteLine(s); // ==> "{6} {56780} {Sample String}"
See IDEONE demo
REGEX matches 2 alternatives inside {...} that is preceded with optional whitespace (\s*):
\s*7[0-9]{12}\s* - optional whitespace followed with7`, then 12 digits and optional whitespace
\d+\.\d+\s* - 1 or more digits, a . decimal separator, again 1 or more digits, and optional whitespace.
Since all your values are inside {...}, you need no word boundaries.