Substring or split word in clamp - c#

I need an solution for my problem. I have a clause like:
Hello guys I am cool (test)
An now I need an effective method to split just only the part in the parentheses and the result should be:
test
My try is to split the String in words like. But I don't think it is the best way.
string[] words = s.Split(' ');

I do not think that split is the solution to your problem
Regex is very good for extracting data.
using System.Text.RegularExpression;
...
string result = Regex.Match(s, #"\((.*?)\)").Groups[1].Value;
This should do the trick.

Assuming:
var input = "Hello guys I am cool (test)";
..Non-Regex version:
var nonRegex = input.Substring(input.IndexOf('(') + 1, input.LastIndexOf(')') - (input.IndexOf('(') + 1));
..Regex version:
var regex = Regex.Match(input, #"\((\w+)\)").Groups[1].Value;

You can use regex for this:
string parenthesized = Regex.Match(s, #"(?<=\()[^)]+(?=\))").Value;
Here's an explanation of the various parts of the regex pattern:
(?<=\(): Lookbehind for the ( (excluded from the match)
[^)]+: Sequence of characters consisting of anything except )
(?=\)): Lookahead for the ) (excluded from the match)

The most efficient way is using string methods but you don't need Split but Substring and IndexOf. Note that this currently just finds a single word in parentheses:
string text = "Hello guys I am cool (test)";
string result = "--no parentheses--";
int index = text.IndexOf('(');
if(index++ >= 0) // ++ used to look behind ( which is a single character
{
int endIndex = text.IndexOf(')', index);
if(endIndex >= 0)
{
result = text.Substring(index, endIndex - index);
}
}

string s = "Hello guys I am cool (test)";
var result = s.Substring(s.IndexOf("test"), 4);

Related

Substring from path string

In strings like this (I get strings from Directory.GetFiles())
string temp = "\\folder_name\\file_name.filetype.[somename#somedomain].wallet"
What is the best way to substring: file_name.filetype
I could do something like this:
const string source = ".[somename#somedomain].wallet";
temp.Substring(0, temp.IndexOf(source, StringComparison.Ordinal));
... but problem is that "mail" in string ".[xxxx#xxxx].wallet" is changing, in my words my string source should be something like this:
const string source = ".[*].wallet"; //so all strings that are in .[all_strings].wallet
Is there an easy way to do something like this (with asterisk "*"), or I will have to substring piece by piece and concatenate this new string?
You can construct a regex that requires a backslash before the substring of interest, and a text in square brackets followed by .wallet at the end.
Here is how you can do with in C# regex APIs:
string temp = #"\folder_name\file_name.filetype.[somename#somedomain].wallet";
var m = Regex.Match(temp, #"(?<=\\)[^.]*\.[^.]*(?=\.\[[^\]]*\].wallet)");
if (m.Success) {
Console.WriteLine(m.Value);
} else {
Console.WriteLine("<no match>");
}
Demo.
(?<=...) and (?=...) constructs are zero-length look-ahead and look-behind. They are not included in the m.Value.
You could search for the 2nd index of . and take everything before that point.
string temp = "\\folder_name\\file_name.filetype.[somename#somedomain].wallet";
var filename = Path.GetFileName(temp);
var lastIndex = filename.IndexOf('.', filename.IndexOf('.') + 1);
var fileYouAreLookingFor = filename.Substring(0, lastIndex);
Working fiddle
You could also use an regex to achieve this. The first group of the following one should be what you are looking for.
string temp = "\\folder_name\\file_name.filetype.[somename#somedomain].wallet";
var filenameRegex = new Regex("^.*\\\\(.*)\\.\\[.*\\]\\.wallet$");
var match = filenameRegex.Match(temp);
var result = match.Groups[1];

In C#, what is the recommended way to parse out a word inside certain characters?

In C#, I have a string coming in that I am reading into a variable that looks like this
var fullString = "Some random text (importantword)"
what is the easiest way to parse out the "importantword"? RegEx? doing just .IndexOf() for the "(" and ")" characters?
IndexOf is definitely the easiest.
https://msdn.microsoft.com/en-us/library/aa287734%28v=vs.71%29.aspx
Followed by a Substring.
var startIndex = fullString.IndexOf("(") + 1;
var endIndex = fullString.IndexOf(")");
var targetWord = fullString.Substring(startIndex, endIndex - startIndex);
EDIT: As pointed out in the comments below, I forgot startIndex was for the opening parenthesis instead of the actual word.
Regular expressions have two drawbacks: they may be slow compared to IndexOf() and they are usually not easy to read and understand. In your case, finding the text in parenthesis is easy and doesn't need regular expressions.
If your string always ends with ), then you can search for just the ( and remove the last character:
var start = fullString.IndexOf('(') + 1;
var end = fullString.Length - 1;
return fullString.Substring(start, end - start);
Otherwise, do both searches. In this last case, remember to use the IndexOf(char, int) overload to avoid searching for the entire string:
var start = fullString.IndexOf('(') + 1;
var end = fullString.IndexOf(')', start); // Notice the `start`
return fullString.Substring(start, end - start);
You could use capturing groups or lookarounds to get all the characters present between () brackets.
String input = #"Some random text (importantword)";
Regex rgx = new Regex(#"(?<=\()[^()]*(?=\))");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[0].Value);
OR
String input = #"Some random text (importantword)";
Regex rgx = new Regex(#"\(([^()]*)\)");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[1].Value);

how to remove special char from the string and make new string?

I have a string 4(4X),4(4N),3(3X) from this string I want to make string 4,4,3. If I am getting the string 4(4N),3(3A),2(2X) then I want to make my string 4,3,2.
Please someone tell me how can I solve my problem.
This Linq query selects substring from each part of input string, starting from beginning till first open brace:
string input = "4(4N),3(3A),2(2X)";
string result = String.Join(",", input.Split(',')
.Select(s => s.Substring(0, s.IndexOf('('))));
// 4,3,2
This may help:
string inputString = "4(4X),4(4N),3(3X)";
string[] temp = inputString.Split(',');
List<string> result = new List<string>();
foreach (string item in temp)
{
result.Add(item.Split('(')[0]);
}
var whatYouNeed = string.Join(",", result);
You can use regular expressions
String input = #"4(4X),4(4N),3(3X)";
String pattern = #"(\d)\(\1.\)";
// ( ) - first group.
// \d - one number
// \( and \) - braces.
// \1 - means the repeat of first group.
String result = Regex.Replace(input, pattern, "$1");
// $1 means, that founded patterns will be replcaed by first group
//result = 4,4,3

Regular expression to replace

I heard it's possible to use regular expression to replace. I have following scenario where I would like to remove index number semicolon and pound sign.
(Index Number;#)
For example 521;#SouthWest Region
after expression it should be Southwest Region
I tried many variation ((?<=^.*?;).* OR ^.*?; ) but not working.
Regex.Replace("521;#SouthWest Region", #"\d+;#", "");
// results SouthWest Region
Ctrl+Shift+H Find what: (.*)\;\#{.*} Replace with: \1
Try this:
public void Replace()
{
var myString = "(In£dex N#£umber;#)";
var replacement = String.Empty;
var regExPattern = #"\d|[#£;]";
var regEx = new Regex(regExPattern);
var result = regEx.Replace(myString, replacement);
Console.WriteLine("The replaced string: {0}", result);
}
Edit: Ooops, sorry, i think I missunderstod your question.
Edit 2: Replace the above code with: var regExPattern = #"\d|[#£;]";

Regular expression to split string and number

I have a string of the form:
codename123
Is there a regular expression that can be used with Regex.Split() to split the alphabetic part and the numeric part into a two-element string array?
I know you asked for the Split method, but as an alternative you could use named capturing groups:
var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
var match = numAlpha.Match("codename123");
var alpha = match.Groups["Alpha"].Value;
var num = match.Groups["Numeric"].Value;
splitArray = Regex.Split("codename123", #"(?<=\p{L})(?=\p{N})");
will split between a Unicode letter and a Unicode digit.
Regex is a little heavy handed for this, if your string is always of that form. You could use
"codename123".IndexOfAny(new char[] {'1','2','3','4','5','6','7','8','9','0'})
and two calls to Substring.
A little verbose, but
Regex.Split( "codename123", #"(?<=[a-zA-Z])(?=\d)" );
Can you be more specific about your requirements? Maybe a few other input examples.
IMO, it would be a lot easier to find matches, like:
Regex.Matches("codename123", #"[a-zA-Z]+|\d+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
rather than to use Regex.Split.
Well, is a one-line only: Regex.Split("codename123", "^([a-z]+)");
Another simpler way is
string originalstring = "codename123";
string alphabets = string.empty;
string numbers = string.empty;
foreach (char item in mainstring)
{
if (Char.IsLetter(item))
alphabets += item;
if (Char.IsNumber(item))
numbers += item;
}
this code is written in java/logic should be same elsewhere
public String splitStringAndNumber(String string) {
String pattern = "(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(string);
if (m.find()) {
return (m.group(1) + " " + m.group(2));
}
return "";
}

Categories