Replace " " with "_" Regex pattern ? c# [closed] - c#

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
So i need to replace all spaces with undescores and if there is no space there it should stay as it is. The only problem i have is the pattern thing can someone post the pattern thing for this problem ?

You didn't gave us any information but seems like you just need to use String.Replace() method.
Returns a new string in which all occurrences of a specified Unicode
character or String in the current string are replaced with another
specified Unicode character or String.
yourstring = yourstring.Replace(' ', '_');
Because i use NetMF there is no string.replace
Are you sure about that? I think String class is avaiable for NetMF
Version Information
Available in the .NET Micro Framework versions 2.0, 2.5, 3.0, 4.0, and 4.1.

If you are really eager to do this using Regex try this :
mystring = Regex.Replace(mystring, #"\s", "_", RegexOptions.None, TimeSpan.FromSeconds(1.5));
Where mystring is the original string.

Related

Replace substring which comes in middle of string only [closed]

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
I want regx pattern in C# which find substring in any string which comes in middle only. Let say ,
Input : "toprohitpop rohittoppop toppoprohit"
find substring : "rohit"
Replace with : "$$$$"
Output : "top$$$$pop rohittoppop toppoprohit"
if substring "rohit" comes in left or right of the string then it should not be replaced.Substring "rohit" will only be replaced when it comes in middle of string .
Thanks in advance.
Use non-word-break anchors:
\Brohit\B
The \B will only match if it is in the middle of a word.
Read about it.
var input = "toprohitpop rohittoppop toppoprohit";
var regex = new Regex(#"\Brohit\B");
var output = regex.Replace(input, "$$$$$$$$");
See "Anchors" in Regular Expression Language.
Also, be careful with the '$' in the substitution string (see comments)
Use the following regex: .+rohit.+
Basically it enforeces at least one char before rohit and one after

Trimming and Removing text from string? [closed]

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
I want to trim a string and remove all the words that occur after a certain word.
For example - If the string contains 'very' text
string mySentence=" Today is very nice day! ";
if (mysentence.Contains(very))
{
//remove everything that starts with 'very' until rest of the line..
}
result should be:
Today is
First you split using the required word
string[] splits = mysentence.Split("very");
Since you've already made certain that "very" is inside the string, this will get you two strings. You want the first one (the split before the "very"). You need to trim the extra space from that one so:
string result = splits[0].Trim();
Try this
string mySentence = " Today is very nice day! ";
if (mySentence.Contains("very"))
{
mySentence = mySentence.Remove(mySentence.IndexOf("very")).Trim();
}

Regular Expression for mandatory and number only [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 am trying to make regular expression that allow only numbers and must be mandatory. no blank field or white space allow using asp regular expression validater with C#. i tried this
ValidationExpression="^[/d]*+$". this is working for number only. blank field accepted by this expression.
Thanks
Be careful that \d can match digits other than 0 to 9, such as Eastern Arabic numerals. I would suggest using:
"^[0-9]+$"
The RegexValidator isn't called when the field is empty, you must use the RequiredFieldValidator in combination : http://msdn.microsoft.com/en-us/library/eahwtc9e%28v=vs.100%29.aspx
Besides, the correct regex would be
^\d+$
\d is for any number
+ is for 1 to n occurences
Take out the "*". The "+" is being applied to the results of the previous expression which is "0 or more digits" and is always found.
ValidationExpression="^[/d]+$"

how to find Substring up to specific character [closed]

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
How do I find a substring up to a specific character? What I would like is to find the substring of something similar to:
172.20.9.93\randDir
I want the IPAddress, or in other terms, everything up until the "\" if the "\" exists.
Is there a way to do this with substring or is there a better way to do this?
I want the IPAddress, or in other terms, everything up until the "\" if the "\" exists.
Two options:
Find the index of the first \ using IndexOf, then use Substring
int firstSlash = text.IndexOf('\\');
string ipAddress = firstSlash == -1 ? text : text.Substring(0, firstSlash);
Split by \ using String.Split, and then take the first part
string ipAddress = text.Split('\\')[0];
Try using a regular expression match:
var input = #"172.20.9.93\rand";
var output = Regex.Match(input, #"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b");
Console.WriteLine(output.Value);

Replace all occurrences of string only if they don't start by '#' [closed]

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
I would like to replace all the occurrences of a string, as long as those don't start by '#', so for example in the following query:
(surname = #surname and surname = #surname1)
if I want to replace surname, it will only replace the two of them on the left side of the equal sign. Thus, leaving #surname and #surname1 unreplaced.
You can use Regex.Replace for this:
Regex.Replace(yourString, "([^#])surname", "$1Diaz");
The [^#] basically tells Regex that any character except the # symbol can come in front of the "surname" text that you are looking for. The $1 is necessary because otherwise, whatever character that is will also get stripped out.
Note that this Regex, without some additional modification, will not match "surname" if it is at the beginning of the string. In the example you provided, it starts with an open-parenthesis, so as long as that condition holds, the solution above will work.

Categories