What i have:
1. 25686-47362-04822-08149-48999-28161-15124-63556
2. 25686-47362-04822-08149-48999-28161-15124-6355654534
3. 54354325686-47362-04822-08149-48999-28161-15124-63556
4. 25686-47362-04822-08149-48999-28161-15124-6355654534fds
5. fdsfds54354325686-47362-04822-08149-48999-28161-15124-63556
6. 25686-47362-04822-08149-48999-28161-15124-63556-63556
What i expect to get
1. 25686-47362-04822-08149-48999-28161-15124-63556
I tried something nearest ([0-9]{5,5}){8}
I trying to avoid 2,3,4,5,6.
Try this
string source = #"25686-47362-04822-08149-48999-28161-15124-63556";
bool result = Regex.IsMatch(source, "^[0-9]{5}(-[0-9]{5}){7}$");
Explanation:
^ anchor (beginning of the string)
[0-9]{5} 5 digits group
(-[0-9]{5}){7} 7 more groups of 5 digits
$ anchor (ending of the string)
I am not sure there is a way to ask for it to "repeat" the grouping, but i would type it like that:
/^([0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5}\-[0-9]{5})/
You can use this:
^\d+\.\s(\d{5}-?){8}$
It matches a whole line that matches your criteria: A digit or more, a dot, a whitespace, 8 blocks à 5 digits with hyphens.
You can qualify that line with:
/^((?:\D|^)\d{5}){8}$/m
Demo
Or
/^((?:-|^)\d{5}){8}$/m
To be more specific with hyphen delimiters.
Related
I have a working regex that matches floating point as well as integer but I need one to only match floating point (number of decimals can be any).
This is what I have so far
Regex regex = new Regex(#"^-?(?=.*[1-9])\d+(\.\d+)?$", RegexOptions.IgnoreCase);
How do I mod it to only match the floating point?
The regex you're looking for is (I've split it into groups for the explanation):
Regex regex = new Regex(#"^(\+?)([0-9]*)(\.)([0-9]+)$");
Explanation:
Group 1 - an optional plus sign at the beginning.
Group 2 - optional digits before the dot (why optional? because, for example, .345 is a valid number - and stands for 0.345).
Group 3 - the decimal dot.
Group 4 - numbers after the dot. One comment: This regex will accept numbers such as 12345.0 although is not a really decimal. I don't see how to solve this just with regex (without code).
I think you need this one
Regex regex = new> Regex(#"^([-+]?)(([0]{1})|([1-9]+([0-9]*)))(\.)([0-9]+)$");
Group 1: Optional "-" sing on the begin.
Group 2 - 5: The number begin whit 0 OR begin whit another num (1-9)
one or more. In this Case is not posible to match (EX: 001.55 - It's
not a float number).
Group 6 - Include "."
Group 7 - Include one or more (0-9)
I am having an issue with identifying a multiple character digit in a string.
What I am attempting to do is check, from right to left, for the first digits for comparison.
My original regex:
Regex.Match(s, #"\d{3}", RegexOptions.RightToLeft);
This would match the first 3 digits it came across in any string. Eg:
hello123 - Output: 123
good234bye - Output: 234
see-you-456-tomorrow - Output: 456
No worries. However, now we're not certain of the length the number might be, so we have changed the Regex to this:
Regex.Match(s, #"\d*", RegexOptions.RightToLeft);
This looks for the first string of digits, any length, and uses that. However, it returns an empty match if the number is not on the end of the string. Eg:
hello12 - Output: 12
good-bye-1234 - Output: 1234
see-1-you-2-morrow - Output: Nothing
How can I look for the first x-length of digits in a string, from right to left and disregarding any non-digit characters, without it returning an empty match?
Quantifiers
A digit repeated once or more times
\d+
A digit repeated from 3 times to 5 times
\d{3,5}
A digit repeated at least 5 times
\d{5,}
You can read more about quantifiers in this tutorial
As you may have realized,
see-1-you-2-morrow
^
|
\d* matches an empty position (here)
between the last character and the end of string
Can you try this? I'm new to Regex, but I guess it works for you!
var s = #"see-you-456-tomorrow";
var r = Regex.Match(s, #"[[\d]]*\d*", RegexOptions.RightToLeft);
Console.WriteLine(r);
You can see it working here.
We have a security issue where a specific field in a database has some sensitive information in it. I need a way to detect numbers that are between 2 and 8 in length, replace the digits with a "filler" of the same length.
For instance:
Jim8888Dandy
Mike9999999999Thompson * Note: this is 10 in length and we don't want to replace the digits
123Area Code
Tim Johnson5555555
In these instances anytime we find a number that is between 2 and 8 (inclusive) then I want to replace/fill/substitute that value with the number 0 and keep the length of the original digits
End Result
Jim0000Dandy
Mike9999999999Thompson
000Area Code
Tim Johnson0000000
Is there an easy way to accomplish this using RegEx?
You need to provide a static evaluator method that would do the replacing. It replaces digits in the match with zeroes:
public static string Evaluate(Match m)
{
return Regex.Replace(m.Value, "[0-9]", "0");
}
And then use it with this code:
string input = "9999999099999Thompson534543";
MatchEvaluator evaluator = new MatchEvaluator(Program.Evaluate);
string replaced = Regex.Replace(input, "(?:^|[^0-9])[0-9]{2,8}(?:$|[^0-9])", evaluator);
The regex is:
(?:^|[^0-9]) - should be at the start or preceeded by non-digit
[0-9]{2,8} - the to capture between 2 and 8 digits
(?:$|[^0-9]) - should be at the end or followed by non-digit
Just for the clever regex department. This is not an efficient regex.
(?<=(?>(?'front'\d){0,7}))\d(?=(?'back'(?'-front'\d)){0,7}(?!\d))((?'-front')|(?'-back'))
Replace to 0.
/(?<=(?>(?'front'\d){0,7})) # Measure how many digits we're behind.
\d # This digit is matched
(?=
(?'back' # Measure how many digits we're in front of.
(?'-front'\d)){0,7}
# For every digit here, subtract one group from 'front',
# As to assert we'll never go over the < 8 digit requirement.
(?!\d) # no more digits
)
(
(?'-front') # At least one capturing group left for 'front' or 'back'
|(?'-back') # for > 2 digits requirement.
)/x
I need a regular expression that will match one or two digits before the dot and one digit after the dot (0 or 5).
I tried it out for at least one hour, and I'm getting mad....
Possible results should be:
5,0
5,5
30,0
30,5 etc.
If just one digit is insert, it want a result as follows :
5 --> 5,0
Is there someone who can help me? Thanks a lot!!
You just want to check for one or two digits, followed by the dot literal, and either '0' or '5'.
^\d{1,2}\.[05]$
That doesn't handle the single digit one, though. There's not an easy way to just match a single digit in the same regex you're matching one or two, so you could use a second regex:
^\d$
Then convert that to a double/float if you get any matches.
Try the following regex...
(?:\d{1,2}(?=.)|(?<=.)[50])
string regex=#"(?<b>\d{1,2})(?<a>[.]0|[.]5)?";
Match m=Regex.Match(input,regex);
string result=m.Groups["b"].Value+","+m.Groups["a"].Value==""?0:m.Groups["a"].Value;
Above code would give these results for input
550.57 => 50,5
644 => 44,0
Greetings beloved comrades.
I cannot figure out how to accomplish the following via a regex.
I need to take this format number 201101234 and transform it to 11-0123401, where digits 3 and 4 become the digits to the left of the dash, and the remaining five digits are inserted to the right of the dash, followed by a hardcoded 01.
I've tried http://gskinner.com/RegExr, but the syntax just defeats me.
This answer, Equivalent of Substring as a RegularExpression, sounds promising, but I can't get it to parse correctly.
I can create a SQL function to accomplish this, but I'd rather not hammer my server in order to reformat some strings.
Thanks in advance.
You can try this:
var input = "201101234";
var output = Regex.Replace(input, #"^\d{2}(\d{2})(\d{5})$", "${1}-${2}01");
Console.WriteLine(output); // 11-0123401
This will match:
two digits, followed by
two digits captured as group 1, followed by
five digits captured as group 2
And return a string which replaces that matched text with
group 1, followed by
a literal hyphen, followed by
group 2, followed by
a literal 01.
The start and end anchors ( ^ / $ ) ensure that if the input string does not exactly match this pattern, it will simply return the original string.
If you can use custom C# scripts, you may want to use Substring instead:
string newStr = string.Format("{0}-{1}01", old.Substring(2,2), old.Substring(4));
I don't think you really need a regex here. Substring would be better. But still if you want regex only, you can use this:
string newString = Regex.Replace(input, #"^\d{2}(\d{2})(\d+)$", "$1-${2}01");
Explanation:
^\d{2} // Match first 2 digits. Will be ignored
(\d{2}) // Match next 2 digits. Capture it in group 1
(\d+)$ // Match rest of the digits. Capture it in group 2
Now, the required digits, are in group 1 and 2, which you use in the replacement string.
Do you even SQL? Pull some levers and stuff.