Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Need to write a regular expression which would match all the files having names like:
ABCD^^A12397_4^^A12397_5^^ABCServer2^^1^^20190117654345
ABCDE^^A12394567_4^^A165557_5^^ABCServer5^^3^^20180117654345
Basically need to match pattern like :
Alphabets^^AlphaNumericWithSpecialChracter^^ANWSC^^ANWSC^^Numeric^^Numeric
Things i have tried :
Regex.IsMatch("ABCD^^A12397_4^^A12397_5^^Server2^^1^^20190117654345", "^ABCD\\^\\^A12397_4\\^\\^A12397_5\\^\\^Server2\\^\\^1\\^\\^20190117654345$");
Thanks .
Try this pattern: ^[A-Za-z](\^\^[\w_]+){3}(\^\^\d+){2}$
Explanation:
[A-Za-z] = Alphabets
(\^\^[\w_]+){3} = ^^AlphaNumericWithSpecialChracter repeated three times (with special character _
(\^\^\d+){2} = ^^Numeric repeated twice
Let's build the regular expression step by step:
Alphabets^^AlphaNumericWithSpecialChracter^^ANWSC^^ANWSC^^Numeric^^Numeric
Assuming
Alphabets - One or more A..Z or a..z letters - [A-Za-z]+
AlphaNumericWithSpecialChracter - One or more A..Z, a..z, 0..9 letters or _ - [A-Za-z0-9_]+
ANWSC - One or more A..Z, a..z, 0..9 letters - [A-Za-z0-9]+
Numeric - One or more 0..9 letters - [0-9]+
Now we should start the pattern with ^ anchor, end it with $ one and combine the chunks with \^\^ (please, note, escapement: we want ^ as a plain character, not as an anchor):
string pattern =
#"^[A-Za-z]+\^\^[A-Za-z0-9_]+\^\^[A-Za-z0-9]+\^\^[A-Za-z0-9]+\^\^[0-9]+\^\^[0-9]+$";
Or if we want to add readability we can put it as
string pattern = string.Join(#"\^\^",
"^" + // String start
"[A-Za-z]+", // Alphabets
"[A-Za-z0-9_]+", // AlphaNumericWithSpecialChracter
"[A-Za-z0-9]+", // ANWSC
"[A-Za-z0-9]+", // ANWSC
"[0-9]+", // Numeric
"[0-9]+" + // Numeric
"$"); // End of string
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this line:
abc cd_ef_1g fff
the 'abc' is a fixed value and always there. The fff can change.
I'm now searching for the following word, which is always second, always between whitespaces or tabs, and always after 'abc'.
cd_ef_1g
It could be with '_' or '-', or numbers.
I've searched all over, found similar results but not exactly with these conditions (using REGEX in C#).
Please help.
According to your description, this should be sufficient:
abc\s+([\w-]+)\s+fff
abc # Always before
\s+ # One or more white-space chars
([\w-]+) # One or more alphanumeric chars ('-' also included)
\s+ # One or more white-space chars
fff # Always after
The captured group is the word sought after.
Demo
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to convert string like
"abcabc"
to
"abc"
Here is what want to achieve
ANDSQLP47ANDSQLP47\CTOPROD8R2
to
ANDSQLP47\CTOPROD8R2
You can use some Regex to find the repeating pattern and do the replacement:
var regex = new Regex(#"(\w+)\1\\(\w+)");
var result = regex.Replace(#"ANDSQLP47ANDSQLP47\CTOPROD8R2", #"$1\$2");
//result: ANDSQLP47\CTOPROD8R2
Regex explanation:
(\w+) : Match sequence of characters (first capture group $1)
\1 : Match same sequence of characters as first capture group
\\ : Match '\' character
(\w+) : Match sequence of characters (second capture group $2)
You can get more info about Regex on MSDN
Edit:
To match a string with two repeated words, I would use the following Regex:
var regex = new Regex(#"^(\w+)\1$");
var result = regex.Replace(#"abcabc", #"$1");
//result: abc
^ and $ denote the start and end of the string, so that it matches only if the whole text is the repetition of two words.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is it possible to concatenate two sub strings from a input string using Regex
Example : Input string "ABTTPQR 00100300250000" and I want take first two characters "AB" and first 9 digits "001003002" and concatenate these two string to one "AB001003002"
Much shorter variation using references:
Regex.Replace("ABTTPQR 00100300250000", #"^(\w{2})\w*\s(\d{9})\d+$", #"$1$2")
// = "AB001003002"
You can do as follows :
Regex regex = new Regex(#"(\w{2})\w*\s(\d{9})\d*");
var matches = regex.Matches("ABTTPQR 00100300250000");
String output = matches[0].Groups[1].Value + matches[0].Groups[2].Value;
//AB001003002
Regex Explanation:
(\w{2})\w*\s(\d{9})\d*
The (\w{2}) will match the first 2 chars, then it will look for more chars and a space \w*\s then it will match first 9 digits (\d{9}) and then will look for more digits \d*.
The () captures the matches into groups which are accessed in c# like shown in code sample: matches[0].Groups[1].Value for group 1 (chars) and matches[0].Groups[2].Value for group 2 (digits)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can you please help me to find a expression to accept these :
C1D3
A1Z5R7
H2L7V5X3
but does not accept these :
A1B2A2 //Because the A repeated 2 times
C4F5F3
C2C1
B1B2F6
I am tring to create a expression to use it in C#.Net
In this sort of problem back-references are you're friend.
so (.).*\1 will match a character followed by anything followed by whatever the first capture group matched
Assuming you have a bunch of Capital Letter/Number pairs...
/^(([A-Z])(?!.*\2)[0-9])+$/g
Breakdown...
/^ # Start regex, and pattern match
( # Start group 1
([A-Z]) # Capture any character from A-Z into group 2
(?!.*\2) # Do negative lookahead `(?!...)` to ensure there is no repeat of the captured group 2 (`\2`), after zero or more of anything (`.*`)
[0-9] # Capture a digit
)+ # End group 1
$/g # End regex, and pattern match, with global flag set
Alternatively, for all tokens to avoid repetition equally...
/^(([A-Z0-9])(?!.*\2))+$/g
You can do it without regex:
StringReader reader = new StringReader(str);
string line;
while((line = reader.ReadLine()) != null)
{
if (line.Distinct().Count() != line.Length)
return false;
}
return true;
You can use this negative lookahead based rgex to avoid matching duplicate containing strings:
/^(?:(.)(?!.*?\1))+$/
/^(?:(.)(?!.*?\1))+$/.test('A1B2D3'); // true
/^(?:(.)(?!.*?\1))+$/.test('A1B2A3'); // false
string pattern=#"\w+";
var uniqueCharWords=Regex.Matches(input,pattern)
.Cast<Match>()
.Where(x=>x.Value.Distinct()==x.Value.Length);
If you are looking for a perfect regex,this is it
string pattern=#"\b(?!\w*(\w)\w*\1\w*)\w+\b";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I need to write a regular expression that will be correct for telephone number with some rules:
1) max length 13 symbols
2) it should start from '+'
3) it should contain sonly numbers [0-9]
So, for example, it should be like this:
+447289347598342745
Regex re = new Regex(#"^\+\d{1,13}$");
^\+ → the string must start with +. Note that + must be escaped because it has special meaning otherwise.
\d → equivalent to the character class [0-9]
{1,13} → there must be at least one and no more than 13 occurences of the digit characters
In .NET, you should either use [0-9] instead of \d, or else you should specify RegexOptions.ECMAScript. Otherwise, the regex will match a string like +୧٢३੪૫ (which contains Unicode digits 1-5 in various scripts). – Michael Liu
So either use:
Regex re = new Regex(#"^\+[0-9]{1,13}$");
// or
Regex re = new Regex(#"^\+\d{1,13}$", RegexOptions.ECMAScript);