I need Regex to detect numbers only between special characters.
Pattern ;\d+=\d+?
String 0014;5010730101000033347=4510120173?AA
My objective is to get this string
;5010730101000033347=4510120173?
The \d+? at the end of the pattern matches 1 digit, no more, due to the +? lazy quantifier matching 1 or more occurrences, but as few as necessary to return a valid match.
You may use
;\d+=\d+\?
^^
C# declaration:
string pattern = #";\d+=\d+\?";
See the regex demo
Details:
; - a semi-colon
\d+ - 1 or more digits
= - an equal sign
\d+ - 1 or more digits
\? - a literal ? char
Related
I want to check string which look like following
1st radius = 120
and
2nd radius = 'value'
Here is my code
v1 = new Regex(#"^[A-Za-z]+\s[=]\s[A-Za-z]+$");
if (v1.IsMatch(singleLine))`
{
...
...
}
Using #"^[A-Za-z]+\s[=]\s[A-Za-z]+$" this expression 2nd string is matched but not first and when used this #"^[A-Za-z]+\s[=]\s\d{0,3}$" then only matched first one.
And i also want to check for radius = 'val01'
Basing on your effort, it looks as if you were trying to come up with
^[A-Za-z]+\s=\s(?:'[A-Za-z0-9]+'|\d{1,3})$
See the regex demo. Details:
^ - start of string
[A-Za-z]+ - one or more ASCII letters
\s=\s - a = char enclosed with single whitespace chars
(?:'[A-Za-z0-9]+'|\d{1,3}) - a non-capturing group matching either
'[A-Za-z0-9]+' - ', then one or more ASCII letters or digits and then a '
| - or
\d{1,3} - one, two or three digits
$ - end of string (actually, \z is safer when it comes to validating as there can be no final trailing newline after \z, and there can be such a newline after $, but it also depends on how you obtain the input).
If the pattern you tried ^[A-Za-z]+\s[=]\s[A-Za-z]+$ matches the second string radius = 'value', that means that 'value' consists of only chars A-Za-z.
In that case, you could either add matching digits to the second character class:
^[A-Za-z]+\s=\s[A-Za-z0-9]+$
If you either want to match 1-3 digits or at least a single char A-Za-z followed by optional digits:
^[A-Za-z]+\s=\s(?:[0-9]{1,3}|[A-Za-z]+[0-9]*)$
The pattern matches:
^ Start of string
[A-Za-z]+\s=\s Match the first part with chars A-Za-z and the = sign (Note that = does not have to be between square brackets)
(?: Non capture group
[0-9]{1,3} Match 1-3 digits (You can use \d{0,3} but that will also match an emtpy string due to the 0)
| Or
[A-Za-z]+[0-9]* Match 1+ chars A-Za-z followed by optional digits
) Close non capture group
$ End of string
Regex demo
I have a string like Acc:123-456-789 and another string like -1234567, I need your help to write an expression to match digits in case there is no separator between the digits.
-*(?!\d*(?:\d*-)$)\d*$
Input strings:
Acc:123-456-789 -12323232 7894596
Desired result:
group 1 12323232
group 2 7894596
I think this ought to work:
(?<=^|\s|\s-)(\d+)(?=\s|$)
Breaking it down:
(?<=^|\s|\s-) - A positive lookbehind that matches the start of the string, whitespace, or whitespace followed by a -.
(\d+) - Matches and captures number sequences.
(?=\s|$) - A positive lookahead that matches whitespace or the end of the string.
** Note: If you need to capture negative number sequences, replace (\d+) with (\-?\d+).
Try it online
Regex reference
Remember for use in C# that you need to escape backslashes or use the # prefix to a string literal (#" ").
This is my string.
19282511~2017-08-28 13:24:28~Entering (A/B)~1013~283264/89282511~2017-08-28 13:24:28~Entering (A/B)~1013~283266/79282511~2017-08-28 13:24:28~Entering (A/B)~1013~283261
I would like this string be split like below:
19282511~2017-08-28 13:24:28~Entering (A/B)~1013~283264
89282511~2017-08-28 13:24:28~Entering (A/B)~1013~283266
79282511~2017-08-28 13:24:28~Entering (A/B)~1013~283261
I cannot split my string blindly by slash (/) since there is a value A/B will also get split.
Any idea of doing this by regex expression?
Your help will definitely be appreciated.
You may split with / that is in between digits:
(?<=\d)/(?=\d)
See the regex demo
Details
(?<=\d) - a positive lookbehind that requires a digit to appear immediately to the left of the current location
/ - a / char
(?=\d) - a positive lookahead that requires a digit to appear immediately to the right of the current location.
Since the \d pattern is inside non-consuming patterns, only / will be removed upon splitting and the digits will remain in the resulting items.
Another idea is to match and capture these strings using
/?([^~]*(?:~[^~]*){3}~\d+)
See this regex demo.
Details
/? - 1 or 0 / chars
([^~]*(?:~[^~]*){3}~\d+) - Group 1 (what you need to grab):
[^~]* - zero or more chars other than ~
(?:~[^~]*){3} - 3 or more sequences of ~ and then 0+ chars other than ~
~\d+ - a ~ and then 1 or more digits.
The C# code will look like
var results = Regex.Matches(s, #"/?([^~](?:~[^~]){3}~\d+)")
.Cast()
.Select(m => m.Groups1.Value)
.ToList();
NOTE: By default, \d matches all Unicode digits. If you do not want this behavior, use the RegexOptions.ECMAScript option, or replace \d with [0-9] to only match ASCII digits.
I am trying to match the following pattern.
A minimum of 3 'groups' of alphanumeric characters separated by a hyphen.
Eg: ABC1-AB-B5-ABC1
Each group can be any number of characters long.
I have tried the following:
^(\w*(-)){3,}?$
This gives me what I want to an extent.
ABC1-AB-B5-0001 fails, and ABC1-AB-B5-0001- passes.
I don't want the trailing hyphen to be a requirement.
I can't figure out how to modify the expression.
Your ^(\w*(-)){3,}?$ pattern even allows a string like ----- because the only required pattern here is a hyphen: \w* may match 0 word chars. The - may be both leading and trailing because of that.
You may use
\A\w+(?:-\w+){2,}\z
Details:
\A - start of string
\w+ - 1+ word chars (that is, letters, digits or _ symbols)
(?:-\w+){2,} - 2 or more sequences of:
- - a single hyphen
\w+ - 1 or more word chars
\z - the very end of string.
See the regex demo.
Or, if you do not want to allow _:
\A[^\W_]+(?:-[^\W_]+){2,}\z
or to only allow ASCII letters and digits:
\A[A-Za-z0-9]+(?:-[A-Za-z0-9]+){2,}\z
It can be like this:
^\w+-\w+-\w+(-\w+)*$
^(\w+-){2,}(\w+)-?$
Matches 2+ groups separated by a hyphen, then a single group possibly terminated by a hyphen.
((?:-?\w+){3,})
Matches minimum 3 groups, optionally starting with a hyphen, thus ignoring the trailing hyphen.
Note that the \w word character also select the underscore char _ as well as 0-9 and a-z
link to demo
I want to match any string that does not contain the string "DontMatchThis".
What's the regex?
try this:
^(?!.*DontMatchThis).*$
The regex to match a string that does not contain a certain pattern is
(?s)^(?!.*DontMatchThis).*$
If you use the pattern without the (?s) (which is an inline version of the RegexOptions.Singleline flag that makes . match a newline LF symbol as well as all other characters), the DontMatchThis will only be searched for on the first line, and only a string without LF symbols will be matched with .*.
Pattern details:
(?s) - a DOTALL/Singleline modifier making . match any character
^ - start of string anchor
(?!.*DontMatchThis) - a negative lookahead checking if there are any 0 or more characters (matched with greedy .* subpattern - NOTE a lazy .*? version (matching as few characters as possible before the next subpattern match) might get the job done quicker if DontMatchThis is expected closer to the string start) followed with DontMatchThis
.* - any zero or more characters, as many as possible, up to
$ - the end of string (see Anchor Characters: Dollar ($)).