Combine TrimStart and TrimEnd for a String - c#

I have a string with some letters and numbers. here an exemple :
OG000134W4.11
I have to trim all the first letters and the first zeros to get this :
134W4.11
I also need to cut the character from the first letter he will encounter to finally retreive :
134
I know I can do this with more than one "trim" but I want to know if there was an efficient way to do that.
Thanks.

If you don't want to use regex.. then Linq is your friend
[Test]
public void TrimTest()
{
var str = "OG000134W4.11";
var ret = str.SkipWhile(x => char.IsLetter(x) || x == '0').TakeWhile(x => !char.IsLetter(x));
Assert.AreEqual("134", ret);
}

Here is the regex I would use
([1-9][0-9]*)[^1-9].*
Here is some C# code you could try
var input = "OG000134W4.11";
var result = new Regex(#"([1-9][0-9]*)[^1-9].*").Replace(input, "$1");

using System;
using System.Text.RegularExpressions;
namespace regex
{
class MainClass
{
public static void Main (string[] args)
{
string result = matchTest("OG000134W4.11");
Console.WriteLine(result);
}
public static string matchTest (string input)
{
Regex rx = new Regex(#"([1-9][0-9]+)\w*[0-9]*\.[0-9]*");
Match match = rx.Match(input);
if (match.Success){
return match.Groups[1].Value;
}else{
return string.Empty;
}
}
}
}

Related

Split string into multiple alpha and numeric segments

I have a string like "ABCD232ERE44RR". How can I split it into separate segments by letters/numbers. I need:
Segment1: ABCD
Segment2: 232
Segment3: ERE
Segment4: 44
There could be any number of segments. I am thinking go Regex but don't understand how to write it properly
You can do it like this;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var substrings = Regex.Split("ABCD232ERE44RR", #"[^A-Z0-9]+|(?<=[A-Z])(?=[0-9])|(?<=[0-9])(?=[A-Z])");
Console.WriteLine(string.Join(",",substrings));
}
}
Output : ABCD,232,ERE,44,RR
I suggest thinking of this as finding matches to a target pattern rather than splitting into the parts you want. Splitting gives significance to the delimiters whereas matching gives significance to the tokens.
You can use Regex.Matches:
Searches the specified input string for all occurrences of a specified regular expression.
var matches = Regex.Matches("ABCD232ERE44RR", "[A-Z]+|[0-9]+");
foreach (Match match in matches) {
Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index);
}
Try something like:
((A-Z)+(\d)*)+
If you decide not to use regex, you can always go the manual route.
const string str = "ABCD232ERE44RR1SGGSG3333GSDGSDG";
var result = new List<StringBuilder>
{
new StringBuilder()
};
char last = str[0];
result.Last().Append(last);
bool isLastNum = Char.IsNumber(last);
for (int i = 1; i < str.Length; i++)
{
char ch = str[i];
if (!((Char.IsDigit(ch) && isLastNum) || (Char.IsLetter(ch) && !isLastNum)))
{
result.Add(new StringBuilder());
}
result.Last().Append(ch);
last = ch;
isLastNum = Char.IsDigit(ch);
}

who to find and separating a special part of text of in TextBox and copy it in a other TextBox with C#?

I have a long text in the multi line TextBox . I will seprarating a part of the text that begining with a special word and end with a other special word . so then copy it in a other TextBox .
please help me .
I suggest you to use regex to resolve this issue.
Below is the codes:
using System;
using System.Text.RegularExpressions;
namespace Regex_GetSpecialPart_Demo
{
class Program
{
static void Main(string[] args)
{
string example = "starttest_exampleend";
var match = Regex.Match(example, #"start(\S+)end");
if (match.Success)
{
var result = match.Result("$1");
Console.WriteLine(result);
}
Console.ReadLine();
}
}
}
The result is "test_example"
Try split
Example :
string data = "TEXTONExxTEXTTWOxxTEXTTHREExxTEXTFOUR";
return data.Split(new string[] { "xx" }, StringSplitOptions.None);
Regex is the best choice, but in case you don't want to use it and prefer to write extra code, use this:
public String getTextBetween(string start, string end, string text) {
int t1 = 0;
int t2 = 0;
if (text == null) return "";
t1 = text.IndexOf(start);
if (t1 >= 0) t2 = text.IndexOf(end, t1 + start.Length);
return text.Substring(t1 + start.Length, t2 - t1 - start.Length);
}

How to extract information from text validated against a regex

I wrote the following regex:
^(\[[0-9]+\])*$
It matches these exemplary texts:
""
"[0]"
"[0][1][2][0][9]"
What I would like to do is to get a list of numbers stored within brackets. How to do this elegantly?
My approach:
public static IEnumerable<int> GetNumbers(string text)
{
if (false == Regex.IsMatch(text, "^(\\[[0-9]+\\])*$"))
throw new FormatException();
return text
.Trim('[', ']')
.Split(new[] {"]["}, StringSplitOptions.None)
.Select(int.Parse);
}
Solution:
public static IEnumerable<int> GetNumbers(string text)
{
var match = Regex.Match(text, "^(\\[(?<number>[0-9]+)\\])*$");
if (!match.Success)
throw new FormatException();
var captures = match.Groups["number"].Captures;
foreach (Capture capture in captures)
yield return int.Parse(capture.Value);
}
If you cannot change the pattern, but only code, you may use
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
var s1 = "[0][1][2][0][9]";
var res1 = string.Join(",", GetNumbers(s1).ToList()); // Many
Console.WriteLine(res1);
Console.WriteLine(string.Join(",", GetNumbers("[8]").ToList())); // One
Console.WriteLine(string.Join(",", GetNumbers("").ToList())); // Empty
}
public static IEnumerable<int> GetNumbers(string text)
{
if (string.IsNullOrWhiteSpace(text))
return Enumerable.Empty<int>(); // No text => return Empty
var match = Regex.Match(text, #"^(\[[0-9]+\])*$");
if (!match.Success) // No match => return Empty
return Enumerable.Empty<int>();
return match.Groups[1].Captures
.Cast<Capture>() // Get the captures
.Select(n => Int32.Parse(n.Value.Trim('[', ']'))); // Trim the brackets and parse
}
}
See the IDEONE demo

C# - Read between parentheses in RichTextBox

So I want to find out what is in between specific parentheses in a richtextbox. For example:
if (richTextBox1.Text.Contains("testname(") {
// Find what is in brackets of testname()
String outcome = //what is in brackets of testname()
}
This may be hard to understand, but let's say this is the richtextbox:
testname(name)
Then the string outcome would be name.
You can use regular expression to get the value between the parenthesis.
string text = richTextBox1.Text; // testname("some text")
string value = Regex.Match(text, #"\(([^)]*)\)").Groups[1].Value;
wrote from iPad so not tested, but something along these lines should get you there
String a = "testname(";
String b = "testname(Val)";
int x = b.IndexOf(a);
If (richTextBox1.Text.Contains(a)){
if (x != -1)
{
string final = b.Substring(x + a.Length);
}
}
string outcome = richTextBox1.Text;
outcome = outcome.Substring(outcome.IndexOf("(")+1, outcome.IndexOf(")") - outcome.IndexOf("("));
Regex would be the way to go.
Here I am matching exactly a parenthesis followed by any character one or more times followed by another parenthesis.
You have to escape parenthesis.
string yourText = richTextBox1.Text;
//string cow = "this is some text(this is the text you want)";
Regex regex = new Regex(#"\(.+\)");
Match match = regex.Match(yourText);
if (match.Success)
{
Console.WriteLine(match.Value); // with cow -> this is the text you want
} // can have else
You may want to consider using * instead of + in case they don't have any input between the ( ) Example: Regex regex = new Regex(#"\(.*\)");
Now it matches any character 0 or more times
You can achieve this with Linq and Regular Expressions.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Propress
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string text = richTextBox1.Text;
ICollection<string> matches =
Regex.Matches(text.Replace(Environment.NewLine, ""), #"\(([^)]*)\)")
.Cast<Match>()
.Select(x => x.Groups[1].Value)
.ToList();
foreach (string match in matches)
MessageBox.Show(match);
}
}
}
string textBox = richTextBox1.Text;
textBox = textBox.Replace("testname(", "");
// Now textBox has the string you are looking for

C# Split string into array based on prior character

I need to take a string and split it into an array based on the type of charcter not matching they proceeding it.
So if you have "asd fds 1.4#3" this would split into array as follows
stringArray[0] = "asd";
stringArray[1] = " ";
stringArray[2] = "fds";
stringArray[3] = " ";
stringArray[4] = "1";
stringArray[5] = ".";
stringArray[6] = "4";
stringArray[7] = "#";
stringArray[8] = "3";
Any recomendations on the best way to acheive this? Of course I could create a loop based on .ToCharArray() but was looking for a better way to achieve this.
Thank you
Using a combination of Regular Expressions and link you can do the following.
using System.Text.RegularExpressions;
using System.Linq;
var str="asd fds 1.4#3";
var regex=new Regex("([A-Za-z]+)|([0-9]+)|([.#]+)|(.+?)");
var result=regex.Matches(str).OfType<Match>().Select(x=>x.Value).ToArray();
Add additional capture groups to capture other differences. The last capture (.+?) is a non greedy everything else. So every item in this capture will be considered different (including the same item twice)
Update - new revision of regex
var regex=new Regex(#"(?:[A-Za-z]+)|(?:[0-9]+)|(?:[#.]+)|(?:(?:(.)\1*)+?)");
This now uses non capturing groups so that \1 can be used in the final capture. This means that the same character will be grouped if its in then catch all group.
e.g. before the string "asd fsd" would create 4 strings (each space would be considered different) now the result is 3 strings as 2 adjacent spaces are combined
Use regex:
var mc = Regex.Matches("asd fds 1.4#3", #"([a-zA-Z]+)|.");
var res = new string[mc.Count];
for (var i = 0; i < mc.Count; i++)
{
res[i] = mc[i].Value;
}
This program produces exactly output you want, but I am not sure wether it's generic enaugh for your goal.
class Program
{
private static void Main(string[] args)
{
var splited = Split("asd fds 1.4#3").ToArray();
}
public static IEnumerable<string> Split(string text)
{
StringBuilder result = new StringBuilder();
foreach (var ch in text)
{
if (char.IsLetter(ch))
{
result.Append(ch);
}
else
{
yield return result.ToString();
result.Clear();
yield return ch.ToString(CultureInfo.InvariantCulture);
}
}
}
}

Categories