Regexp - Delete the one word before XXX, remove XXX too [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 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]+", "");

Related

How do I remove an X number of capital letters from a string? [closed]

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
I want to remove an X number of capital letters from a string.
For example if I had the strings:
string Line1 = "NICEWEather";
and
string Line2 = "HAPpyhour";
How would I create a function that pulls out the 2 sets of Capital letters?
Try using regular expressions with \p{Lu} for a Unicode capital letter
using System.Text.RegularExpressions;
...
// Let's remove 2 or more consequent capital letters
int X = 2;
// English and Russian
string source = "NICEWEather - ХОРОшая ПОГОда - Keep It (HAPpyhour)";
// ather - шая да - Keep It (pyhour)
string result = Regex.Replace(source, #"\p{Lu}{" + X.ToString() + ",}", "");
Here we use \p{Lu}{2,} pattern: capital letter appeared X (2 in the code above) or more times.
To remove capital letter from string
string str = " NICEWEather";
Regex pattern = new Regex("[^a-z]");
string result = pattern.Replace(str, "");
Console.WriteLine(result );
output: ather
to remove capital letter if occurrences more than once in sequence order then try this
string str = " NICEWEather";
Regex pattern = new Regex(#"\p{Lu}{2,}");
string output = pattern.Replace(str, "");
Console.WriteLine(output);

regex to remove digits, allow only lowercase letters, remove special characters [closed]

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
Here is my code :
private void txtMot_KeyUp(object sender, KeyEventArgs e)
{
/* Clear message */
txtMessage.Clear();
Regex regex = new Regex(#"\d+");
Match match = regex.Match(txtMot.Text);
if (match.Success)
{
/* Text in red*/
txtMessage.Foreground = Brushes.Red;
/* Message text*/
txtMessage.Text = "Only letters";
}
}
I managed to remove all the digits.
I'm wondering now, how can I make it to :
Remove the digits.
Allow only lowercase letters.
Remove any kind of special characters(_+ù$é)
How can I do it please?
you can simply use the regex [a-z] and test with different set of data, as it accepts only the lowercase letters
public static void Main()
{
string test = "_+ù$é"; //change this to any set of test data
Regex regex = new Regex(#"[a-z]");
Match match = regex.Match(test);
if (match.Success)
{
Console.WriteLine("Matched");
}
else
Console.WriteLine("Not Matched");
}
dotNetFiddle
EDIT:
The above snippet would fail in a scenario where;
string test = "_+ù$é abc";
^ as this contains both the special letters and the lowercase, if you only want the lowercase letters to be accepted then;
replace this:
Regex regex = new Regex(#"[a-z]");
with this pattern:
Regex regex = new Regex(#"^([a-z]{1,25})$"); //this makes sure the string
// is only of lowercase
// letters and does not contain any digits
// or special chars
dotNetFiddle

Compare two strings in c# containing random URL [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 7 years ago.
Improve this question
I need to compare the below strings. The problem I have is the url in both strings will be different every time e.g:
www.google.com
http://www.google.com
google.co.uk!
So contains cannot match the strings because of the URL not matching.
String1 = "This is my string http://www.google.co.uk and that was my url"
String2 = "this is my string google.gr and that was my url"
So I basically want to compare the contents of the string minus the URl, each string can contain different text each time so looking for the URL at the same location each time will not work.
I have searched extensively on here for an answer to this problem, but I was unable to find a working solution.
Thanks in advance
Use regular expressions to remove links:
String string1 = "This is my string http://www.google.co.uk and that was my url";
String string2 = "this is my string http://google.gr and that was";
Regex rxp = new Regex(#"http://[^\s]*");
String clean1 = rxp.Replace(string1, "");
String clean2 = rxp.Replace(string2, "");
And now you can compare clean1 with clean2. OFC regexp above is just an example it'll just remove url's staring with "http://". You may need something more sophisticated, based on your real data.
Using Regular Expressions:
Regex regex = new Regex(#"\s((?:\S+)\.(?:\S+))");
string string1 = "This is my string http://www.google.co.uk and that was my url.";
string string2 = "this is my string google.gr and that was my url.";
var string1WithoutURI = regex.Replace(string1, ""); // Output: "This is my string and that was my url."
var string2WithoutURI = regex.Replace(string2, ""); // Output: "this is my string and that was my url."
// Regex.Replace(string1, #"\s((?:\S+)\.(?:\S+))", ""); // This can be used too to avoid having to declare the regex.
if (string1WithoutURI == string2WithoutURI)
{
// Do what you want with the two strings
}
Explaining the regex \s((?:\S+)\.(?:\S+))
1. \s Will match any white space character
2. ((?:\S+)\.(?:\S+)) Will match the url until the next white space character
2.1. (?:\S+) Will match any non-white space character without capturing the group again (with the ?:)
2.2. \. Will match the character ".", because it will always exist in a url
2.3. (?:\S+)) Again, will match any non-white space character without capturing the group again (with the ?:) to get everything after the dot.
That should do the trick...

Compare regular expression [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 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);

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