Regex to replace special characters with specific sign [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 6 years ago.
Improve this question
I'm trying to use Regex to change my URL from this:
http://localhost:51577/Item/92MM+BLACK+CASE+FAN+W%2f+3+PIN+CONNECTOR+-+Cool+%26+Quiet/222069843383
Into a URL that would look like this:
http://localhost:51577/Item/92MM-BLACK-CASE-FAN-W-2f-3-PIN-CONNECTOR-Cool-26-Quiet/222069843383
Any %, + or +-+ sign would be replaced with - sign using regex. I think regex is the best solution for this, but I'm not so familiar with writing regex expressions... Can someone help me out with this?
Edit: Guys I have an even better idea... I have the Title name in controller in following format:
92MM BLACK CASE FAN W/ 3 PIN CONNECTOR - Cool & Quiet
How could I write an regex to replace white spaces and remove any extra white space (if there are any) in the string array...
Edit 2: Basically replacing any special character with a - sign... Any ideas?

https://msdn.microsoft.com/en-us/library/e7f5w83z(v=vs.110).aspx
Regex.Replace(YOUR_STRING, "[^0-9a-zA-Z]+", "-");
Try that

Related

Is there any specific way to check if there's only one letter in regex? [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 last year.
Improve this question
I wanted to find a regex pattern that checks if there's one letter in between the space.
For example: John A Doe
I want to capture only John Doe (without A) but there's 50/50 chance that the data will not contain middle initials.
I made this pattern ([A-z]* [A-z] [A-z]*|[A-z]* [A-z]*) but it captures also the middle one.
I'm sorry for the vague title cause I'm really confused rn.
Edit: I forgot about the [A-z] captures upto 122 in ascii table. I replaced it with \w as well.
Perhaps code like:
var m = Regex.Match(input, #"(?<f>[a-z]+)( [a-z])? (?<l>[a-z]+)", RegexOptions.IgnoreCase);
Console.WriteLine(m.Groups["f"].Value);
Console.WriteLine(m.Groups["l"].Value);
You'll need to capture the first and last names separately and stick them back together later

Regular expression generation for a string [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 want to create a regular expression to check the following string pattern, I tried following these tutorials but it's still confusing. Any help is appreciated.
Type: In Folder
(T or t)ype(spaces or no space):(spaces or no space)(i or I)n(spaces or multiple space)(f or F)older
So following your desired pattern at the end of your question:
(t|T)ype\s*:\s*(i|I)n\s*(f|F)older
The above pattern should match your string. Mind you, \s* is zero to unlimited spaces, which means it would match on the string you provided even if there were no spaces present; if there will always be at least one space present, you can replace them with \s+
Hope this helps!
(T|t)ype\s*:\s*(I|i)n\s*(f|F)older

RegEx match specific pattern in a text "[d-n]" [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
What would be a regex for matching "[d-n]", where n is any number?
i.e.
Test_4_[d-123] - returns ideally only 123
or, if I can return [d-123] I could make some string formatting.
(?<=\[d-)\d+(?=\]) will return 123 from Test_4_[d-123].
You give very little information as to how you want stuff matched.
A very simple solution could be:
\[d-(\d+)\]
From left to right:
\[ will match the literal character [
d- will match just d-
(\d+) will match any digit one or more times. It is in parenthesis, which makes it a capture/match group. This should mean that if you are using a regex tool/library, you should be able to retrieve the "first match group" and you should retrieve just 123.
\] will match the literal character ]
I can only suggest using a website like https://regex101.com/ which can help in creating regular expressions.

One white space only [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
I need one white space only in my regular expressen.
How should I create the code that validate if one white space is availabe in the string value?
I'm not sure exactly what you mean but I'm guessing you want to check for exactly one whitespace, but any number of non-whitespace characters:
#"^\S*\s\S*$"
Example code:
Regex regex = new Regex(#"^\S*\s\S*$");
Console.WriteLine(regex.IsMatch("Hello, world!"));
Console.WriteLine(regex.IsMatch("This contains three spaces."));
Console.WriteLine(regex.IsMatch("Two\nlines."));
Output:
True
False
True
Other variations
To check if the string contains exactly one whitespace only (no other characters):
#"^\s$"
To check if the string contains at least one whitespace:
#"\s"

C# Regex Replace How to append text to end of each line(C#) [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 2 years ago.
Improve this question
I have searched the site for this simple problem but cant find an answer.
I have a multiline string . I want to add a constant string to end of each line.
I am using Regex.Replace but facing problems. I tried to replace as the following.
Pattern Replace With
-------------------------------------------
$ Text
($) Text$1
\n Text
\n Text\n
(\n) Text$1
But none of these work. In all the cases the multiple lines are joined into a single line.
How can I accomplish this ?
You should be able to do:
string newString = oldString.Replace("\r\n", "Text\r\n");
replace Text with the string you want to append to the end of each line

Categories