Am struggling in writing the regular expression pattern for the below string. I used the below pattern to get the matched string. But, i got the error.
Note: The input string may be anyone of the below input string.
string input = "IN-7874 - hello";
// or "IN-7874 - Hello"
// "IN-7874 - 1) hello"
// "IN-7874 - 1. hello"
// "IN-7874 - 1)hello"
// "IN-7874 - 1.hello"
string pattern = #"^[A-Z]+\\-^[0-9]\s+\\-\\s+^[A-Z]"; //[any number of capital letters]hyphen[any number of numbers(0-9)]space[hyphen]space[numbers or strings]
var a = Regex.Match(input, pattern);
Could anyone please help me on this?
My output should be in the pattern form of [any number of capital letters]hyphen[any number of numbers(0-9)]space[hyphen]space
Example: {SAM-123 - }// don't consider the curly brace.
You can use
^[A-Z]+-[0-9]+\s+-\s+(?:[0-9]+[.)]\s*)?[A-Za-z]+
See the regex demo
Explanation:
^ - start of string
[A-Z]+ - 1 or more uppercase ASCII letters
- - a hyphen
[0-9]+ - 1 or more digits
\s+ - 1+ whitespaces
- - a hyphen
\s+ - see above
(?:[0-9]+[.)]\s*)? - an optional sequence of:
[0-9]+ - 1+ digits
[.)] - a literal . or )
\s* - 0+ whitespaces
[A-Za-z]+ - 1 or more ASCII letters
Related
I have string like (CAT,A)(DOG,C)(MOUSE,D)
i want to get the DOG value C using Regular expression.
i tried following
Match match = Regex.Match(rspData, #"\(DOG,*?\)");
if (match.Success)
Console.WriteLine(match.Value);
But not working could any one help me to solve this issue.
You can use
(?<=\(DOG,)\w+(?=\))?
(?<=\(DOG,)[^()]*(?=\))
See the regex demo.
Details:
(?<=\(DOG,) - a positive lookbehind that matches a location that is immediately preceded with (DOG, string
\w+ - one or more letters, digits, connector punctuation
[^()]* - zero or more chars other than ( and )
(?=\)) - a positive lookahead that matches a location that is immediately followed with ).
As an alternative you can also use a capture group:
\(DOG,([^()]*)\)
Explanation
\(DOG, Match (DOG,
([^()]*) Capture group 1, match 0+ chars other than ( or )
\) Match )
Regex demo | C# demo
String rspData = "(CAT,A)(DOG,C)(MOUSE,D)";
Match match = Regex.Match(rspData, #"\(DOG,([^()]*)\)");
if (match.Success)
Console.WriteLine(match.Groups[1].Value);
}
Output
C
I have this code , and I would like to match this pattern:
All characters a-z , A-Z between two "" .
The variables should be between square brackets.
How can I achieve that?
This input should be accepted :
["a1","a2"]
This is my code and what I've tried:
string text = "["a1","a2"]";
Regex rg2 = new Regex(#"[^\""a-z|A-Z|0-9\""$]+(, [^\""a-z |A-Z |0-9\""$]+)+");
if (rg2.IsMatch(text)
Console.WriteLine("True");
You can use
Regex rg2 = new Regex(#"^\[""[a-zA-Z0-9]+""(?:,\s*""[a-zA-Z0-9]+"")*]$");
Regex rg2 = new Regex(#"\A\[""[a-zA-Z0-9]+""(?:,\s*""[a-zA-Z0-9]+"")*]\z");
See the C# demo:
string text = "[\"a1\",\"a2\"]";
Regex rg2 = new Regex(#"\A\[""[a-zA-Z0-9]+""(?:,\s*""[a-zA-Z0-9]+"")*]\z");
Console.WriteLine(rg2.IsMatch(text)); // => True
Details:
\A - start of string
\[" - a [" fixed string
[a-zA-Z0-9]+ - one or more ASCII letters/digits
" - a double quotation mark
(?:,\s*"[a-zA-Z0-9]+")* - zero or more repetitions of
, - a comma
\s* - zero or more whitespaces
"[a-zA-Z0-9]+" - ", one or more ASCII alphanumeric chars, "
] - a ] char
\z - the very end of string.
I need a regex for an input that contains positive and negative numbers and sometimes a string between " and ". I'm not sure if this can be done in only one pattern. Here's some test cases for the pattern:
*PATH "C:\Users\User\Desktop\Media\SoundBanks\Ambient\WAV_Data\AD_SMP_SFX_WIND0.wav"
*NODECOLOR 0 255 140
*FILEREF -7
*FREQUENCY 22050
The idea would be to use a pattern that returns:
C:\Users\User\Desktop\Media\SoundBanks\Ambient\WAV_Data\AD_SMP_SFX_WIND0.wav
0 255 140
-7
22050
The content always goes after the character *. I've split this in two patterns because I don't know how to do it all in one, but doesn't work:
MatchCollection NumberMtaches = Regex.Matches(FileLine, #"(?<=[*])-?[0-9]+");
MatchCollection FilePathMatches = Regex.Matches(FileLine, #"/,([^,]*)(?=,)/g");
You may read the file into a string and run the following regex:
var matches = Regex.Matches(filecontents, #"(?m)^\*\w+[\s-[\r\n]]*""?(.*?)""?\r?$")
.Cast<Match>()
.Select(x => x.Groups[1].Value)
.ToList();
See the .NET regex demo.
Details:
(?m) - RegexOptions.Multiline option on
^ - start of a line
\* - a * char
\w+ - one or more word chars
[\s-[\r\n]]* - zero or more whitespaces other than CR and LF
"? - an optional " char
(.*?) - Group 1: any zero or more chars other than an LF char, as few as possible
"? - an optional " char
\r? - an optional CR
$ - end of a line/string.
I want to replace "whole" decimal numbers not followed by pt with M.
For example, I need to replace 1, 12, and 36.7, but not 45.63 in the following.
string exp = "y=tan^-1(45.63pt)+12sin(-36.7)";
I have already tried
string newExp = Regex.Replace(exp, #"(\d+\.?\d*)(?!pt)", "M");
and it gives
"y=tan^-M(M3pt)+Msin(-M)"
It does make sense to me why it works like this, but I need to get
"y=tan^-M(45.63pt)+Msin(-M)"
The problem with the regex is that it is still matching a portion of the decimal value 45.63, up to the second-to-last decimal digit. One solution is to add a negative lookahead to the pattern to ensure that we only assert (?!pt) at the real end of every decimal value. This version is working:
string exp = "y=tan^-1(45.63pt)+12sin(-36.7)";
string newExp = Regex.Replace(exp, #"(\d+(?:\.\d+)?)(?![\d.])(?!pt)", "M");
Console.WriteLine(newExp);
This prints:
y=tan^-M(45.63pt)+Msin(-M)
Here is an explanation of the regex pattern used:
( match and capture:
\d+ one or more whole number digits
(?:\.\d+)? followed by an optional decimal component
) stop capturing
(?![\d.]) not being followed by another digit or dot
(?!pt) not followed by pt
Hi there if you need the out put as
"y=tan^-M(Mpt)+Msin(-M)"
then then newExp should be
string newExp = Regex.Replace(exp, #"(\d+\.?\d*)", "M");
if output is
"y=tan^-M(45.63pt)+Msin(-M)"
then newExp should be
string newExp = Regex.Replace(exp, #"(\d+\.?\d*)(?![.\d]*pt), "M");
I think you may assert the point in a string where there are no digits and dots directly followed by "pt":
\b(?![\d.]+pt)\d+(?:\.\d+)?
See the online demo
\b - Match a word-boundary.
(?![\d.]+pt) - Negative lookahead for 1+ digits and dots followed by "pt".
\d+ - 1+ digits.
(?: - Open non-capture group:
\.\d+ - A literal dot and 1+ digits.
)? - Close non-capture group and make it optional.
See the .NET demo
I have the following regex, which is not working:
#"^[a-z]{1}[a-z0-9\-_(%i)]*$"
The user is allowed to use %i, but only in this combination. Only % is not allowed. The expression in parentheses does not work.
The user input could be for example:
testing123%i
testing123
testing-%i-123
But this is not allowed:
testing%123
A character class only matches 1 single char. You need to take %i out of the character class if you want to match %i as a sequence:
^[a-z](?:[a-z0-9_-]|%i)*$
See the regex demo
Details:
^ - start of a string
[a-z] - a lowervase ASCII letter
(?:[a-z0-9_-]|%i)* - zero or more occurrences of:
[a-z0-9_-] - a lowercase ASCII letter, a digit, _ or -
| - or
%i - a literal char sequence %i
$ - end of string.
string pattern = #"\b(?!(?:.\B)(.)(?:\B.)\1)[%i]+\b";
string input = "testing123%i";
if (Regex.IsMatch(input, pattern))
{
return true;
}