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.
Related
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 (#" ").
sorry for such a direct question but i've spent a little too long trying to find a suitable RegEx that can alter the following strings:
01.10
10.01
setting them as:
1.10
10.1
So basically always remove the first '0' in the complete sequence before each period, or in the last sequence.
Is this possible with RegEx as currently it doesn't seem so?
Try this:
find: (^|\.)0+
replace: $1
See here a demo
Note: if the expression is not at the beginning of the string, you should not use ^, but the word boundary \b, like this:
(\b|\.)0+
eventually, double escape it:
(\\b|\.)0+
See other demo
Perhaps you could try it using this regex. This will not match the zero in 0.0 or 0.1 but only when there are digits after the leading zero(s).
\b0+(?=\d\.\d+\b)|(?<=\b\d+\.)0+(?=\d+\b)
\b word boundary
0+(?=\d\.\d+\b) match a zero and use a positive lookahead to assert that the zero is followed by a digit, dot, one or more digits and a word boundary
| Or
(?<=\b\d+\.)0+(?=\d+\b) Positive lookbehind that asserts that what is on the left is a wordboundary, one or more digits and a dot. Then match one or more zeroes and assert that what follows id one or more digits and a wordboundary.
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 write a regexp to get multiple matches of the first character and next three digits. Some valid examples:
A123,
V322,
R333.
I try something like that
[a-aA-Z](1)\d3
but it gets me just the first match!
Could you possibly show me, how to rewrite this regexp to get multiple results?Thank you so much and Have a nice day!
Your regex does not work because it matches:
[a-aA-Z] - an ASCII letter, then
(1) - a 1 digit (and puts into a capture)
\d - any 1 digit
3 - a 3 digit.
So, it matches Y193, E103, etc., even in longer phrases, where Y and E are not first letters.
You need to use a word boundary and fix your pattern as
\b[a-aA-Z][0-9]{3}
NOTE: if you need to match it as a whole word, add \b at the end: \b[a-aA-Z][0-9]{3}\b.
See the regex demo.
Details:
\b - leading word boundary
[a-aA-Z] - an ASCII letter
[0-9]{3} - 3 digits.
C# code:
var results = Regex.Matches(s, #"\b[a-aA-Z][0-9]{3}")
.Cast<Match>()
.Select(m => m.Value)
.ToList();
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