Trimming all leading and trailing namespaces after splitting the word [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a scenario in C#:
Input is->
Sachin.Dutta : trimbegging space :: delete spaceafter
Expected output->
Sachin.Dutta:trimbegging space::delete spaceafter
First,I need to split the string based on : or ::
Then trim whitespaces from beginning and end of every word
Then combine the words to get expected output.
I can write code using for loop to separate words and again recombine them.But,is there any better way using LINQ or Regex for this ?

Use Regex with pattern #"\s*:\s*"
var input = "Sachin.Dutta : trimbegging space :: delete spaceafter";
var result = Regex.Replace(input, #"\s*:\s*", ":");
Output
Sachin.Dutta:trimbegging space::delete spaceafter
Explanation
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
: matches the character : literally (case sensitive)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

Related

Regex: Check If String Contains A Single Instance of the Letter "A" or "B" [closed]

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 4 months ago.
Improve this question
I am completely new to the world of Regular Expressions, and was wondering if someone could provide me with some assistance on getting an expression going.
In my scenario, I need to check if a string contains one letter, and the one letter can either be an A or B. Only phrases with a single letter as A or B are permitted.
Ideally the expression would identify the "Good" values as matches and reject the "Bad" values due to containing multiple letters and not a single A or B.
Any help would be very much appreciated
Thanks!
If you just need the result and regex is not mandatory, you could use a simple expression as below
bool result = word.Where(Char.IsLetter).Count() == 1 && (word.Contains('A') || word.Contains('B'));
The expression ^[^A-Z]*[AB][^A-Z]*$ matches a string containing exactly one letter that is either A or B.
Explanation:
^ Matches the start of the string.
[^A-Z] Matches any character that is not a lette
* Means zero or more of the previous item, thus
[^A-Z]* Matches zero or more characters that are not letters
[AB] Matches either an `A` or a `B`
[^A-Z]* Matches zero or more characters that are not letters
$ Matches the end of the string
If the string should contain at least one character before and after the A or B then the pattern should be modified to be ^[^A-Z]+[AB][^A-Z]+$. Using the + means matching be one or more of the previous item whereas the * means zero or more.
The pattern [A-Z] matches any letter. [^A-Z] matches any character that is not a letter. Similarly [AB] matches either an A or a B. [^AB] matches any character that is not A or B, but this pattern is not needed here. Putting these together gives t

regex for first occurring char semicolon or empty space or single quote [closed]

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 2 years ago.
Improve this question
I have been trying to make a regex expression which can match all the below strings. I was able to make a regex for first two, but unable to do for the next 2. The regex should be such that the matching is performed only until the end of error message.
RAISERROR 20001 #errmsg;
RAISERROR 20001 #errmsg
RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg'
RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg';
Below is the test link:
https://regex101.com/r/ntywvP/3
((?i)raiserror)\s*\d{5,6}\s*([^;|\s*]*)
Any help is appreciated. Thank you in advance.
You could replace ([^;|\s*]*) by (#[^;\r\n]*). You could try the following regex.
(?i)raiserror\s*\d{5,6}\s*(#[^;\r\n]*)
Demo
I would use this:
\bRAISERROR \d+ #(?:'[\w ]+'|[\w ]+);?
Demo
I deal with the optional single quoted message by using an alternation which allows either for this, or for a message sans single quotes.
My suggestion is https://regex101.com/r/ntywvP/4
^((?i)raiserror)\s*\d{5,6}\s*(.+?)(;|\s*)$
Capturing the string beginning with # is done via matching a s few characters as possible with (.+?). This allows specifying possible characters after the wildcard before the end of the line.
^ matches line start
$ matches line end
You can match all between the single quotes or match only non whitespace chars. If you don't need the capturing group around ((?i)raiserror) you can omit it.
(?i)raiserror\s*\d{5,6}\s*#(?:'[^'\r\n]*'|\S+);?
Explanation
(?i) case insensitive modifier
raiserror\s*\d{5,6}\s* Match raiserror and 5-6 digits between optional whitespace chars
# Match the # char
(?: Non capture group for the alternation
'[^'\r\n]*' Match any char except ' between an opening and closing '
| Or
\S+ Match 1+ non whitespace chars
);? Close group and match optional ;
Regex demo
Based on your question you can use either:
'.'
This matches everything in the world with at least one character, so it will match all four of your strings.
Or to match the four strings exactly:
'RAISERROR 20001 #errmsg|RAISERROR 20001 #errmsg|RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg|RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg'
You haven't specified the rules for what strings you want to match and what strings you don't. Any other solutions require guessing on what you mean but haven't explained.

Check if string contains only lowercase letters and symbols [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have to check if string contains only lowercase letters, from 'd' onwards, and the symbols '{', '}', '|', '#'.
I have tried to create Regex but it matches substrings which is not the desired behaviour (the second test mustn't match in the example which I add). I don't know how to explain that the order isn't important and the strings must contain only of the allowed characters. Regex101
[d-z]+[{}|#]+
This regex matches arx#vkdww#qrw#sdvv and this isn't right. How can I fix it?
Thank you in advance!
Your pattern [d-z]+[{}|#]+ matches 1+ times a char d-z in a character class followed by one of the chars listed in the character class [{}|#]+
You have to use anchors to assert the start ^ and end $ of the string and use 1 character class containing all the allowed characters instead of 2:
^[d-z{}|#]*$
Regex demo

Reqular expression for underscore C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need a Reqular expression to identify string in following format in my C# code. The string will always start with "REG" and contain 3 underscores with 2 words and one number between the underscores. See below example:
Example: "REG_SOFTWARE_SECURITY_1234"
I used below REGEX expression suggested by your forums:
"\b[a-zA-Z0-9_]+\b"
But it passes the incorrect inputs also like:
REG_1234
So, it should only pass input in format - "REG_SOFTWARE_SECURITY_1234" Any suggestions?
I dont see issue with your regex. You might be using it incorrectly in c#.
Try this
var str="ALPHABET_ ALPHABET _ ALPHABET _99";
var res = Regex.Matches(str,#"\b[a-zA-Z0-9_]+\b");
foreach (Match match in res)
{
Console.WriteLine(match.Value);
}
Fiddle Here
The backslashes in your search string are not being interpreted as you expect. The C# string "\b[a-zA-Z0-9_]+\b" starts and ends with a "backspace" 0x0008 character.
To match the example string you need to use either "\\b[a-zA-Z0-9_]+\\b" or #"\b[a-zA-Z0-9_]+\b" as the regular expression.
If the task is to match a string with two words and one number separated by three underscores then the following should work:
#"\b[a-zA-Z]+_[a-zA-Z]+_[a-zA-Z]+_[0-9]+\b"
Did you mean a letter by ALPHABET ?
then you could try \w+_ \w+ _ \w+ _\d+, if you intended whitespaces, and you are searching for words, not just letters.
If you want to find a pattern like Letter Underscore Blank etc. , try \w_ \w _ \w _\d.

Need Regex pattern to validate password pattern [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
We need to validate password against following pattern.
“Xabcdef99*” [1st char uppercase, 2nd to 7th chars lowercase, 8th to 9th digits and last char a symbol ]
Can someone provide me Regex for the same? How I can validate following password against that Regex in C#.
Userpcs12* --> Valid
Testeur333 --> Invalid (because last char is not symbol)
userpcs12* --> Invalid (because first char is not uppercase)
You may try this,
^[A-Z][a-z]{6}\d{2}[~!##$%^&*]$
Add the symbols you want inside the last character class.
or
^[A-Z][a-z]{6}\d{2}\W$
\W matches any non-word character. Change this to [\W_] if you treat _ as special charcater.
Try this:
Regex regex = new Regex(#"^[A-Z][a-z]{6}\d{2}\W$");
Assert.IsTrue(regex.IsMatch("Xabcdef99*"));
Assert.IsTrue(regex.IsMatch("Xabcdef99$"));
Assert.IsFalse(regex.IsMatch("Testeur333"));
Assert.IsFalse(regex.IsMatch("userpcs12*"));
Check this regex:
\A\p{Lu}\p{Ll}{6}\d{2}[~!##$%^&*]\z
\p{Lu} matches uppercase letters
\p{Ll} matches lowercase letters
Demo on regex101

Categories