Compare regular expression [closed] - c#

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 9 years ago.
Improve this question
I want to compare following string by Regular Expression. I have surf lot of time but unable to get it's pattern.
string str = "Full Name: Atif Mahmood"
+ "ID Number: 12345678901"
+ "Mobile Number: +921234567890";
In above string
Full Name:
ID Number:
Mobile Number:
are necessary with sequence and there should be any string after these constants.

var regex = "Full Name:(.*)ID Number:(.*)Mobile Number:(.*)";
var match = Regex.Match(string, regex);
match.Groups[1] will contain the name, [2] will contain the ID number, etc. (Groups[0] is the whole match group, so counting each match starts at 1)
This probably needs some bullet proofing, but you get the idea?

In case you want to check that the string follows the pattern you stated, this expression should do it:
const string expression = "Full Name:.*ID Number:.*Mobile Number:.*";
bool correct = Regex.IsMatch(str, expression);

Related

how to split base64 text into base64 strings c# [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 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a base64 text example:
UAAAAAAAAAA=AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=KAAAAAAAAAA=AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==
I need to split it into sides like this:
UAAAAAAAAAA=
AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
KAAAAAAAAAA=
AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==
I tried:
string base64Text = "UAAAAAAAAAA=AA....."
Regex regex = new Regex(#"^[a-zA-Z0-9+/]*={0,2}$");
string[] base64Strings = regex.Split(base64Text);
You could split on a position where there is a char A-Z or 0-9 to the left and not a = char or the end of the string to the right:
(?<=[A-Z0-9]=)(?!=|$)
Regex demo
string base64Text = #"UAAAAAAAAAA=AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=KAAAAAAAAAA=AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==";
Regex regex = new Regex(#"(?<=[A-Z\d]=)(?!=|$)");
string[] base64Strings = regex.Split(base64Text);
foreach (string s in base64Strings)
{
Console.WriteLine(s);
}
Output
UAAAAAAAAAA=
AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
KAAAAAAAAAA=
AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==

Regexp - Delete the one word before XXX, remove XXX too [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 5 years ago.
Improve this question
I need to remove a word XXX in a string and also one word before XXX.
How I can do that with C# Regexp?
Do a single regex replacement:
string input = #"Hello World XXX Goodbye XXX Rabbit!";
Regex rgx = new Regex(#"\s*\w+\s+(?:XXX|xxx)"); // or maybe [Xx]{3}
string result = rgx.Replace(input, "", 1);
Console.WriteLine(result);
Hello Goodbye XXX Rabbit!
Demo
This replacement only would target XXX for removal if it be preceded by a word (one character or more). Explore the demo to see how it would behave with various inputs.
We can also make the search pattern case insensitive via this:
Regex rgx = new Regex(#"\s*\w+\s+XXX", RegexOptions.IgnoreCase);
^^^^^ add this
U can use the replace method :
String s = "aaa bbb";
s = s.Replace("a", "")
// The example displays the following output:
// The initial string: 'aaa bbb'
// The final string: 'bbb'
Or use a Regex in replace :
tmp = s.Replace(n, "[^0-9a-zA-Z]+", "");

How can I return the index of the second word in a string that can have multiple white spaces followed by one word followed by multiple white spaces? [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 8 years ago.
Improve this question
String str =" vol ABCD C XYZ";
I want to return the index of "A"
Note: There are multiple white spaces before all words including the first.
This will get the index of A in your original example (String str =" vol ABCD C XYZ";):
int indexOfABCD = str.IndexOf(str.Trim()[str.Trim().IndexOf(' ')+1]);
If you have something like String str = " vol ABCD C XYZ"; where there are multiple spaces:
string secondword = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)[1];
int indexOfAinABCD = str.IndexOf(secondword.First());
If you want to get the indices of all the letters in the second word:
IEnumerable<int> fullindex = Enumerable.Range(indexOfAinABCD, secondword.Length);
EDIT:
This will fail if you have A anywhere else in the first word. You should get an exact match through Regex:
int indexOfAinABCD = Regex.Match(str, string.Format(#"\W{0}\W",secondword)).Index+1;
IEnumerable<int> fullindex = Enumerable.Range(indexOfAinABCD, secondword.Length);
So something like String str = " vABCDol ABCD C XYZ"; won't be an issue

Removing a letter before a capital letter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a few names as strings that have an 'x' before their last name that needs to be removed.
So, for example, 'John xSmith' needs to come back as 'John Smith', but not affecting a name like 'Jane x Doe'.
Use the regular expression (?<=\s)x(?=[A-Z]) to remove all of the x's followed by a capital letter and after a whitespace.
This is not use regex but it will achieve what you are trying to do (including allowing names such as Jane x Doe)
static void Main(string[] args)
{
string name = "John xSmith";
var result = new StringBuilder();
string[] splitString = name.Split(' ');
foreach (string partName in splitString)
{
if (partName.Length > 1 && partName.StartsWith("x"))
{
result.Append(partName.Substring(1));
}
else
{
result.Append(partName);
}
result.Append(" ");
}
Console.WriteLine(result.ToString().Trim());
Console.ReadKey();
}
With name = "John xSmith" would produce John Smith
With name = "Jane x Doe" would produce Jane x Doe
Does this regEx match your string? ^([a-zA-Z ]+)x([A-Z][a-zA-Z ]+)*$
After, you have just to use the 2 match between parenthesis to rebuild your string without the 'x'

What does the regular expression search/seplace pattern do? [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 8 years ago.
Improve this question
What does the <matcher>?() regular expression do when used in the context of a search replace?
string input = "z=""(?<matcher>([a-z]{3,15}))"""
string pattern = z="cat"
string replacement = #"<ANIMAL>${matcher}</ANIMAL>";
string formattedOutput = Regex.Replace(input, pattern, replacement);
The formattedOutput will be "cat" after the expression has evaluated.
You have a lot of errors...
Here is the correction:
string pattern = #"z=\""(?<matcher>([a-z]{3,15}))\""";
string input = #"z=""cat""";
string replacement = #"<ANIMAL>${matcher}</ANIMAL>";
string formattedOutput = Regex.Replace(input, pattern, replacement);
Console.WriteLine(formattedOutput);
?<matcher> is just a named group. You can pick any name. For example the following is equivalent:
string pattern = #"z=\""(?<WHATEVER>([a-z]{3,15}))\""";
string input = #"z=""cat""";
string replacement = #"<ANIMAL>${WHATEVER}</ANIMAL>";
string formattedOutput = Regex.Replace(input, pattern, replacement);
Console.WriteLine(formattedOutput);

Categories