I Have a string "User " + nick + " has been connected" (WinForms)
Can you please tell me, how to get nick from that string ? (it will be printed as 1 string)
It's behind "User" and "has" words.
Thanks
You can use regex with ^User (.*?) has been connected$
var myUser = Regex.Match(inputString, "^User (.*?) has been connected$").Groups[1].Value;
This could do:
string nickname = yourString.Replace("User ", "").Replace(" has been connected", "");
The boilerplate text is constant so read past the 5 characters of "user " upto the length of the string less the length of the characters you always know will be there.
string name = text.Substring(5, text.Length - 24);
Related
I want regex that string, but I really dont know how. I have figured out how I can get the numbers, but not the other strings
string text = "1cb07348-34a4-4741-b50f-c41e584370f7 Youtuber https://youtube.com/lol love youtube";
string regexstring = "[a-z0-9]+-[a-z0-9]+-[a-z0-9]+-[a-z0-9]+-[a-z0-9]*(?<id>)"
code
Match m = Regex.Match(text, regexstring);
if(m.Success)
Console.WriteLine(m.Groups[0]);
Output
1cb07348-34a4-4741-b50f-c41e584370f7
now I want that the output is that
1cb07348-34a4-4741-b50f-c41e584370f7
Youtuber
https://youtube.com/lol
love youtube
what I finished is the first line of the output but I dont know how to regex the other strings
([\w]+-){5} is cleaner to replace what you already did.
\w means [a-zA-Z0-9_].
Then, if your string always has a website preceded and followed by a number of words separated by spaces, you can do this:
string regexstring = "((\w*-){4})(\w*) (.+?)[A-Za-z]?(https://[^ ]+?) (.+)";
Ouput
Match m = Regex.Match(text, regexstring);
if(m.Success)
Console.WriteLine(m.Groups[1] + "" + m.Groups[2] + "" + m.Groups[3] + "\n" + m.Groups[4] + "\n" + m.Groups[5] + "\n" + m.Groups[6]);
I'm guessing that, if our inputs would look like the same, this expression might be somewhat close to what you might have in mind, not sure though:
^(\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b)\s+(.*?)\s+[A-Z](https?:\/\/\S+)\s+(.*)$
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.
Reference
Searching for UUIDs in text with regex
So, I got an regular expression :
(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])
That should found all letters and replace it with a blank.
var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])", " ");
But when I got for example :
45a, nomDoc become 45 a, while I juste want 45
Did I write this regex wrong? I'm not very good at it, but I was thinking I was good for this one.
The regex must replace all non-numeric characters, following a numeric character or all non-numeric char before numeric.
45a or a45 must give me 45.
Thank you.
What you're doing, is searching for a spot where the string changes from digits to letters or from letters to digits and insert a space there. So yes, 45a becomes 45 a.
If you want to replace all letters with a blank, use
var nomDoc = Regex.Replace(arr[0], "[A-Za-z]", " ");
But I doubt that this is what you want.
If you want to remove all letters, replace with an empty string instead of a space.
If you want to replace all letters following a digit with a space, use
var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");
Try this:
var str = "1 oo 23ksls 4910fsj2jd43ld fkkd ^&?&;#";
var nomDoc = str.Replace('/([^0-9]|\n)/g', ' ');
This replaces all the non-number characters(letters, whitespaces and characters) with a space.
You could try this :
var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");
If you are using Javascript, here's a fiddle :
var Str = "blablabla22445543__-_-_-_-_-_-èèpzofez5zsqef*f-e+ffnfuf'3";
var nomDoc = Str.replace(/[^0-9]/g, "");
$("#result").html(nomDoc);
http://jsfiddle.net/ZqF6L/
It's not quite clear if you want to replace all non-numeric characters with spaces or just remove then completely.
Depending on that, either
var nomDoc = Regex.Replace(arr[0], "[^0-9]", " ");
or
var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");
should do what you want.
hey if you want to remove all your words then use below format method
var demo= Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");
How can I properly use substring, at least, make it work ?
I want to find the position of this substring '#fff'\"> in the stringString this.style.backgroundColor='#fff'\">Choose a translation mode,
I'm doing this :
string substrToSearch = "'#fff'\\\">";
int substrPosition = stringString.IndexOf(substrToSearch);
Unfortunately, substrPosition = -1, and it is not working.
May someone help me ?
EDIT SOLVED :
I don't realy understand why I can't do this string quote = " " "; but I can do this quote = " \" " ,in fact I know that I can but.. here, the "\" is used as an escaping character, so why in the substring that I'm searching it is not interpreted as an escape character but as simple text ?
To me , string substrToSearch = "'#fff'\">";, substrToSearch is '#fff'"> as \" = ".
To be simple, why \" is not interpreted as an escape character ?
try this:
string substrToSearch = "'#fff'\">";
the stringString is not ecapsed when you access it
Here you go...
[TestMethod]
public void StringIndex
{
var stringString = "this.style.backgroundColor='#fff'\">Choose a translation mode,:";
var substrToSearch = "'#fff'\">";
var substrPosition = stringString.IndexOf(substrToSearch);
}
The answer to your added question is fundamental to the concept of string escaping. The syntax string quote = " " "; is problematic because a compiler would think that the string ended with the second quote and not the third one. String escaping allows representing an actual quotation mark with \", so the actual value of " \" " is ", not \".
I have the following code:
var hyphens = myString.Split('-');
topTitle = hyphens[0].Replace("_", " ");
subTitle = hyphens[1].Replace("_", " ");
This is working but if myString does not have two words with a hyphen between them I get an error. How can I make it such that if the myString contains only one word then topTitle gets that word and subTitle gets "n/a"?
You should check if (hyphens.Length == 2) after you split it. Any length other than 2 means you do not have two words.
You could use Regex, but changing your code a little bit helps also.
var hyphens = myString.Split('-');
topTitle = hyphens[0].Replace("_", " ");
subTitle = hyphens.Length > 1 ? hyphens[1].Replace("_", " ") : "n/a";
Just check the Length, if it's less than one show them "n/a" like you want.
I want to convert more that spaces in a string to through c#?
Like if string is
My name is this.
then output should be
My name is this.
Replace the "regular" space with the "non-breaking space" Unicode character:
string outputString = "Input text".Replace(" ", "\u00A0");
Try with RegEx if you need to convert multiple spaces to a single non-breaking-space:
string convertedText =
new Regex("[ ]{2,}").Replace(textToConvert, " ");
Example:
My Name is this
^ ^^^ ^
It'll be changed to:
My Name is this
UPDATE
If you need to preserve extra spaces (and to replace with nbsp only multiple spaces) you may use this regex:
string convertedText =
new Regex(" (?= )|(?<= ) ").Replace(textToConvert, " ");
Example:
My Name is this
^ ^^^ ^
It'll be changed to:
My Name is this
For the second case, as alternative, you may even do not use regex at all (just loop) but they should be faster if you have to do it often with the same regex.
Correction the line below will not work
Please use Server.HtmlEncode for it
You will have to do it by code
string s = " ";
if(s == " ")
{
s = " "
}
Or use "My name is this".Replace(" ", " ");
Try this
string myString = "My name is this".Replace(" ", " ");