I'm real close but I'm not a regex expert. Here's my input strings.
DCIN : 11.896V
5V : 4.988V
Vcom : 0.008V
5VStby: 4.992V
48V : 0.042V
48I : 0mA
I want only the numeric values after the colon. This is what I have so far
/[^\D]+\.?[^\D]+/
and it's also grabbing the two 48 instances and it isn't getting the 0
Your regex requires at least 2 digits on end, that is why it does not match the zero on the last line, and there is no restriction in your pattern to only match after a colon.
Use
var res = Regex.Matches(s, #":\s*(\d*\.?\d+)")
.Cast<Match>()
.Select(m=>m.Groups[1].Value)
.ToList();
See the regex demo
Details:
:\s* - a colon and zero or more whitespace
(\d*\.?\d+) - Capturing group 1 holding the value you need:
\d* - zero or more digits
\.? - an optional dot
\d+ - one or more digits.
I'd do this
^[^:]*:\s*(\d+(?:\.\d*)?)
Related
I want to match an 8 digit number. Currently, I have the following regex but It is failing in some cases.
(\d+)\1{6}
It matches only when a number is different at the end such as 44444445 or 54444444. However, I am looking to match cases where at least 7 digits are the same regardless of their position.
It is failing in cases like
44454444
44544444
44444544
What modification is needed here?
It's probably a bad idea to use this in a performance-sensitive location, but you can use a capture reference to achieve this.
The Regex you need is as follows:
(\d)(?:.*?\1){6}
Breaking it down:
(\d) Capture group of any single digit
.*? means match any character, zero or more times, lazily
\1 means match the first capture group
We enclose that in a non-capturing group {?:
And add a quantifier {6} to match six times
You can sort the digits before matching
string input = "44444445 54444444 44454444 44544444 44444544";
string[] numbers = input.Split(' ');
foreach (var number in numbers)
{
number = String.Concat(str.OrderBy(c => c));
if (Regex.IsMatch(number, #"(\d+)\1{6}"))
// do something
}
Still not a good idea to use regex for this though
The pattern that you tried (\d+)\1{6} matches 6 of the same digits in a row. If you want to stretch the match over multiple same digits, you have to match optional digits in between.
Note that in .NET \d matches more digits than 0-9 only.
If you want to match only digits 0-9 using C# without matching other characters in between the digits:
([0-9])(?:[0-9]*?\1){6}
The pattern matches:
([0-9]) Capture group 1
(?: Non capture group
[0-9]*?\1 Match optional digits 0-9 and a backreference to group 1
){6} Close non capture group and repeat 6 times
See a .NET Regex demo
If you want to match only 8 digits, you can use a positive lookahead (?= to assert 8 digits and word boundaries \b
\b(?=\d{8}\b)[0-9]*([0-9])(?:[0-9]*?\1){6}\d*\b
See another .NET Regex demo
I want to use regular expression (regex) to find invalid characters in a string. The string is a user input and when the regex finds invalid characters I want to give the user feedback which characters where invalid. Example warning message: "Only 0-9 and whitespace allowed. Found invalid characters: ab" when input was "- 10 a 0 b".
A valid string is:
integer
negative or positive
is allowed to have any amount of whitespace at any position.
So for example those VALID strings should NOT match the regex:
"-100"
"- 1 00"
" - 1 00"
"100"
" 1 0 0 "
"1 00"
While the regex should find matches in these INVALID strings:
"- 1 a 0 0 b" should match "a" and "b"
"- 1 a 0 0 -" should match "a" and "-"
I had a working regex for positive integers, until i found out that I forgot to include negative integers:
var regex = new Regex(#"[^0-9\s]")
var invalidCharacters = regex.Matches(text)
I have only very basic knowledge of regex. I tried out negating the regex to include negative integers, but it is not working:
new Regex(#"(?!-?[0-9\s])")
I hope someone can help me with this. If this can be solved easier by removing the whitespace requirement. Then please feel free to ignore the whitespace part.
I would approach this by thinking about the positive case first - which strings are valid? And then negate that with a negative lookaround.
I think this meets your requirements:
(?!\s*-?[\d\s]).
\s* will match any whitespace at the start
-? will optionally match a hyphen
[\d\s] will match numbers and whitespace
(?!expression) is a negative lookaround to negate the whole expression
The . at the end is a way to generate matches. The negative lookaround is just an assertion - it doesn't return any results.
It produces the desired results for the test cases in your question.
You may use
var invalidCharacters = Regex.Matches(text, #"[^0-9\s-]|(?<!^\s*)-");
See the regex demo (modified a bit as the demo is a test against a single multiline string.)
The regex matches:
[^0-9\s-] - a char other than an ASCII digit, any Unicode whitespace char or a - char
| - or
(?<!^\s*)- - a - char that is not preceded with the start of string any any 0+ whitespace chars.
I have a text block that is formatted like this:
1.2.3.4.5 or 1.2222.3.4.5 or 1 or 1.2 etc
An unknow number of numbers and dots (sections of a legal document)
How can I capture the full section (1.2.3.4.5) into a group?
I use C# but any regex is fine, aI can translate it.
UPDATED
Use this Regex:
Regex.Matches(inputString, #"\d[\.\d]*(?<!\.)");
explain:
\d digits (0-9)
[.\d]* any character of: '.', digits (0-9)
(0 or more times, matching the most amount possible))
(?<! subexpression) Zero-width negative lookbehind assertion.
string s = "1.2.3.4.5 or 1.2222.3.4.5 or 1 or 1.2 or 2222.3333.111.5 etc";
var matches = Regex.Matches(s, #"\d+(\.\d+)*").Cast<Match>()
.Select(m => m.Value)
.ToArray();
well, if you know you can't go beyond 5, then you can do
#"1+((.2+)((.3+)((.4+)(.5+)?)?)?)?"
and you can expand on that pattern for every symbol, up to a finite number of symbols
the + means any number of occurrences of the symbol, but at least 1. IF 0 is valid, you can use * instead
put ?: after an opening parenthesies if you don't want the pattern to be captured
like example: (?:abc)
I ommitted them to make the regex more readable.
the ? after the parenthesies, means 1 or 0 of the preceding symbol.
Now if you don't know how far you string can go, for instance
"1.2.3.4......252525.262626.272727.......n.n.n" than my intuition tells me that you can't do that with regex.
I have a phone number field with the following regex:
[RegularExpression(#"^[0-9]{10,10}$")]
This checks input is exactly 10 numeric characters, how should I change this regex to allow spaces to make all the following examples validate
1234567890
12 34567890
123 456 7890
cheers!
This works:
^(?:\s*\d\s*){10,10}$
Explanation:
^ - start line
(?: - start noncapturing group
\s* - any spaces
\d - a digit
\s* - any spaces
) - end noncapturing group
{10,10} - repeat exactly 10 times
$ - end line
This way of constructing this regex is also fairly extensible in case you will have to ignore any other characters.
Use this:
^([\s]*\d){10}\s*$
I cheated :) I just modified this regex here:
Regular expression to count number of commas in a string
I tested. It works fine for me.
Use this simple regex
var matches = Regex.Matches(inputString, #"([\s\d]{10})");
EDIT
var matches = Regex.Matches(inputString, #"^((?:\s*\d){10})$");
explain:
^ the beginning of the string
(?: ){10} group, but do not capture (10 times):
\s* whitespace (0 or more times, matching the most amount possible)
\d digits (0-9)
$ before an optional \n, and the end of the string
Depending on your problem, you might consider using a Match Evaluator delegate, as described in http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx
That would make short work of the issue of counting digits and/or spaces
Something like this i think ^\d{2}\s?\d\s?\d{3}\s?\d{4}$
There are variants : 10 digits or 2 digits space 8 digits or 3 digits space 3 digits space 4 digits.
But if you want only this 3 variants use something like this
^(?:\d{10})|(?:\d{2}\s\d{8})|(?:\d{3}\s\d{3}\s\d{4})$
New to regex syntax here. Trying to write a regex to provide some input validation.
What I need is a regex to match a whole number, or a decimal with exactly one digit past the decimal.
Good Match
1
12
100
1.1
100.1
1.0
No Match
1.22
1.
0
012
Here is what I came up with but it doesn't work:
Regex.IsMatch(paidHours, "\\d+[.]?[0-9]?")
You can try with:
Regex.IsMatch(paidHours, "^\\d+(\\.\\d)?$")
Regex.IsMatch(paidHours, #"^\d+(\.\d)?$")
Edited answer after OP question edit.
Regex.IsMatch(paidHours, #"^[1-9][0-9]*(\.[0-9])?$");
Explanation:
^ : Start of the String
[1-9] : A single number between 1 and 9
[0-9]* : Zero or more number(s) between 0 and 9
([0-9]? would match zero or one number and the String "100" would not match the regex)
( : Start of a group
\. : A point
[0-9] : A single number between 0 and 9
)? : End of the group. The group must be repeated zero or one time
$ : End of the String
Please note that \d is not exactly equivalent to [0-9]: \d matches any unicode digit. For instance, this character ௮ will be matched if you use \d but won't be if you use [0-9].
Try to specify beggining/end of the line:
#"^\d+[.]?[0-9]?$"
You regex won't work since ie 1.234 was a match wit 1.2, if you don't specify you want the string ends with the '$' sign.