I would like to make a function that break lines from a string.
eg.: "1. " -> "\n1. "
so i could write a code like this
string Input = "1. First option";
Input += "2. Second option";
Input += "3. Third option";
Output = WriteMenu(Input);
and get a string like this
"1. First option
\n2. Second option
\n3. Third option"
The pattern will always be [number][dot][whitespace].
It's not a problem if the first option came with new line.
Give this guy a shot
Input = Regex.Replace(Input, #"(?<!^)(\d+\.)", "\n$1")
Regex rgx = new Regex("(\\d+\\.\\s)");
String replaced = rgx.Replace(Input, Environment.NewLine + "$1");
A bit shorter expression like this would work too:
Regex.Replace(Input, #"(?!^)\d+\.", "\n$0")
Related
Here is the code:
string StringFromTheInput = TextBox1.Text; string[] splichar1 =
Regex.Split(StringFromTheInput, #"(?<=[.])\n\r+");
The input:
First sentence. Second sentence. Third sentence.
I want this output:
First sentence.
Second sentence.
Third sentence.
So the main point is to put every splitted text with period mark into new line of textbox and not a continuous text. The code only splits the texts but does not put the sentences in new line.
If you're only looking for periods, specifically, it doesn't really look like you need Regex. You could just write something like:
TextBox1.Text = TextBox1.Text.Replace(". ", "." + Environment.NewLine);
If you need to support other punctuation marks or you want to use Regex for some other reason, you could either use Regex.Replace():
TextBox1.Text = Regex.Replace(TextBox1.Text, #"[.?!] ", "$&\r\n");
Or use Regex.Split() and then re-join the sentences:
var sentences = Regex.Split(TextBox1.Text, #"(?<=[.?!]) ");
TextBox1.Text = string.Join(Environment.NewLine, sentences);
// Or...
//TextBox1.Lines = sentences;
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
i have the following sample cases :
1) "Sample"
2) "[10,25]"
I want to form a(only one) regular expression pattern, to which the above examples are passed returns me "Sample" and "10,25".
Note: Input strings do not include Quotes.
I came up with the following expression (?<=\[)(.*?)(?=\]), this satisfies the second case and retreives me only "10,25" but when the first case is matched it returns me blank. I want "Sample" to be returned? can anyone help me.
C#.
here you go, a small regex using a positive lookbehind, sometime these are very handy
Regex
(?<=^|\[)([\w,]+)
Test string
Sample
[10,25]
Result
MATCH 1
[0-6] Sample
MATCH 2
[8-13] 10,25
try at regex101.com
if " is included in your original string, use this regex, this will look for " mark as well, you may choose to remove ^| from lookup if " mark is always included or you may choose to leave it as it is if your text has combination of with and without " marks
Regex
(?<=^|\[|\")([\w,]+)
try at regex101.com
As far as I can tell, the below regex should help:
Regex regex = new Regex(#"^\w+|[[](\w)+\,(\w)+[]]$");
This will match multiple words, or 2 words (alphanumeric) separated by commas and inside square brackets.
One Java example:
// String input = "Sample";
String input = "[10,25]";
String text = "[^,\\[\\]]+";
Pattern pMod = Pattern.compile("(" + text + ")|(?>\\[(" + text + "," + text + ")\\])");
Matcher mMod = pMod.matcher(input);
while (mMod.find()) {
if(mMod.group(1) != null) {
System.out.println(mMod.group(1));
}
if(mMod.group(2)!=null) {
System.out.println(mMod.group(2));
}
}
if input is "[hello&bye,25|35]", then the output is hello&bye,25|35
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]+", " ");
I have a string which I want to manipulate. Problem is that the replacement contains also regular expressions.
var result = Regex.Replace("Abc", "\r\nAbc\r\n", "\r\n");
// or something like that, it can be also \t and so on...
But the result is not a newline, but the string "\r\n".
PS: By the way, if I want to replace something by nothing, as very simple example:
Regex.Replace("abc", "abc", "")
regex seems to fail. Cannot strings are replaced by an empty string?
You can just use as follows instaed of using regex.
string result = "abc".Replace("abc", string.empty);
Try this:
var result = Regex.Replace("Abc", "\r\nAbc\r\n", System.Environment.NewLine);
Try adding the string literal # identifier before your regex.
var result = Regex.Replace("Abc", #"\r\nAbc\r\n", "\r\n");
My guess it that they are being read in as escape characters before they get to the regex engine.
Basically, the pattern getting passed in is this:
"
Abc
"
Intead of this:
"\r\nAbc\r\n"