I've Table URL and I'd like select Table Name only. What is the best approach to achieve it?
URLs:
"db://SQL Table.Table.[dbo].[DirectoryType]"
"db://SQL Table.Table.[dbo].[IX_AnalysisResult_ConceptVariations]"
"db://SQL Table.Table.[dbo].[IX_AnalysisResult_DocXConcepts]"
DESIRED OUTPUT:
DirectoryType
IX_AnalysisResult_ConceptVariations
IX_AnalysisResult_DocXConcepts
NOTE: These URLs will have db://SQL Table.Table.[dbo]. in common most of the time so I am using following code to achieve this:
CODE:
var trimURL = tableURL.Replace("db://SQL Table.Table.[dbo].", String.Empty).Replace("[",String.Empty).Replace("]",String.Empty);
OUTPUT:
DirectoryType
IX_AnalysisResult_ConceptVariations
IX_AnalysisResult_DocXConcepts
If for some reason URL prefix is changed then my code won't work. So what is the best way to get a table name from these type of URLs?
You could get the last index of '[' and ']' and get the substring therein:
var startIndex = tableUrl.LastIndexOf('[') + 1; // +1 to start after opening bracket
var endIndex = tableUrl.LastIndexOf(']');
var charsToRead = (startIndex - endIndex) - 1; // -1 to stop before closing bracket
var tableName = tableUrl.Substring( startIndex, charsToRead );
Of course, this assumes you can guarantee no brackets in your table name.
References:
String.Substring
String.LastIndexOf
You can use this regex to match the last thing inside the last group of [] that appears immediately at the end of a string:
\[([^\[^\]]*)\]$
At input db://SQL Table.Table.[dbo].[DirectoryType] you grab the string DirectoryType.
The $ symbol means the end of a string.
You can see it in action here.
An example:
var match = new System.Text.RegularExpressions.Regex(#"\[([^\[^\]]*)\]$", RegexOptions.Singleline);
Match match_result = match.Match("db://SQL Table.Table.[dbo].[DirectoryType]");
string result = "";
if (match_result.Groups.Count > 1)
result = match_result.Groups[1].Value;
//result = "DirectoryType"
Remember using System.Text.RegularExpressions;
var matcher = new System.Text.RegularExpressions.Regex(#"^.*\[(?<table>.*?)\]""$", RegexOptions.Compiled);
var results = matcher.Match(/*your input string*/);
Inspect the results in the debugger and you'll find how to extract what you are looking for.
Note that this pattern assumes that your data actually includes the quotation marks shown in your question.
you were doing it right, i just used split on '.', I am assuming your url contains minimum anything.[DirectoryType]"
string op = tableURL.Split('.')[tableURL.Split('.').Length - 1].Replace("[", "").Replace("]", "");
Related
string temp = "12345&refere?X=Assess9677125?Folder_id=35478";
I need to extract the number 12345 alone and I don't need the numbers 9677125 and 35478.
What regex can I use?
Here is the regex for extracting 5 digit number in the beginning of the string:
^(\d{5})&
If length is arbitrary:
^(\d+)&
If termination pattern is not always &:
^(\d+)[^\d]
Based on the Sayse's comment you can simply rewrite as:
^(\d+)
and in case of the termination is some number(for instance 999) then:
^(\d+)999
You don't need regex if you only want to extract the first number:
string temp = "12345&refere?X=Assess9677125?Folder_id=35478";
int first = Int32.Parse(String.Join("", temp.TakeWhile(c => Char.IsDigit(c))));
Console.WriteLine(first); // 12345
If the number you want is always at the beginning of the string and terminated by an ampersand (&) you don't need a regex at all. Just split the string on the ampersand and get the first element of the resulting array:
String temp = "12345&refere?X=Assess9677125?Folder_id=35478";
var splitArray = String.Split('&', temp);
var number = splitArray[0]; // returns 12345
Alternatively, you can get the index of the ampersand and substring up to that point:
String temp = "12345&refere?X=Assess9677125?Folder_id=35478";
var ampersandIndex = temp.IndexOf("&");
var number = temp.SubString(0, ampersandIndex); // returns 12345
From what you haven given us this is fairly simple:
var regex = new Regex(#"^(?<number>\d+)&");
var match = regex.Match("12345&refere?X=Assess9677125?Folder_id=35478");
if (match.Success)
{
var number = int.Parse(match.Groups["number"].Value);
}
Edit: Of course you can replace the argument of new Regex with any of the combinations Giorgi has given.
I'm trying to remove a certain bit of text within a string.
Say the string I have contains html elements, like paragraph tags, I created some sort of tokens that will be identified with "{" at the beginning and "}" at the end.
So essentially the string I have would look like this:
text = "<p>{token}</p><p> text goes here {token3}</p>"
I'm wondering is there a way to extract all the words including the "{}" using C#-Code within the string.
Whilst each token could be different to the next, that is why i must use "{" and "}" to identify them as seen below
At the moment I'm got to this code:
var newWord = text.Contains("{") && word.Contains("}")
Something like
var r = new Regex("({.*?})");
foreach(var match in r.Matches(myString)) ...
The ? means that your regex is non-greedy. If you omit it you´ll simply get everythinbg between the first { and the last }.
Alternativly you may also use this:
var index = text.IndexOf("{");
while (index != -1)
{
var end = text.IndexOf("}", index);
result.Add(text.Substring(index, end - index + 1));
index = text.IndexOf("{", index + 1);
}
I would just use a regex for this:
Regex reg = new Regex("{.*?}");
var results = reg.Matches(text);
The regex searches for any characters between { and }.
The .*? means match any character but in a non greedy way. So it will search for the shortest possible string between braces.
This is probably a super easy question but I can't seem to figure it out. Do you know how to extract just the portion after the '/' in a string. So for like the following:
[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]
So I just want the 'OrderConfirmation_SynergyWorldInc' portion. I got 271 entries where I gotta extract just the end portion (the all have the portions before that in all of them, if that helps).
Thanks!!
IF YOU HAVE A SINGLE ENTRY...
You need to use LastIndexOf with Substring after a bit of trimming:
var s = #"[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]";
s = s.Trim('[',']');
Console.Write(s.Substring(s.LastIndexOf('\\') + 1));
Result: OrderConfirmation_SynergyWorldInc
IF YOU HAVE MULTIPLE RECORDS...
You can use a regex to extract multiple matches from a large text containing [...] substring:
[^\\\[\]]+(?=\])
See demo
For [HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc][SOMEENTRY] string, you will then get 2 results:
The regex matches
[^\\\[\]]+ - 1 or more characters other than ], [ and \
(?=\]) - before the end ] character.
C#:
var results = Regex.Matches(s, #"[^\\\[\]]+(?=\])").OfType<Match>().Select(p => p.Value).ToList();
var s = #"[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]";
Console.WriteLine (s.Trim(']').Split('\\').Last());
prints
OrderConfirmation_SynergyWorldInc
var key = #"[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]";
key = key.Replace("[", string.Empty);
key = key.Replace("]", string.Empty);
var splitkeys =key.Split('\\');
if (splitkeys.Any())
{
string result = splitkeys.Last();
}
Try:
string orderConfirmation = yourString.Split(new []{'\\\'}).Last();
You may also want to remove the last character if the brackets are included in the string.
string pattern = ".*\\(\w*)";
string value = "[HKEY_LOCAL_MACHINE\SOFTWARE\4YourSoul\Server\ReportEMailService\OrderConfirmation_SynergyWorldInc]"
Regex r = new Regex(pattern);
Match m = r.Match(value);
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);
I have a string of type "24;#usernamehere,#AWRFR\user,#,#,#usernamehere"
I want to split this string on the first appearance on # and , i.e i want a string to be fetched which is inbetween these two characters.
So for the above string i want the OUTPUT as:
usernamehere
How can i split a string in between two characters using Regex function?
A simple Regex Pattern might do the job:
var pattern = new System.Text.RegularExpressions.Regex("#(?<name>.+?),");
test:
string s = #"24;#usernamehere,#AWRFR\user,#,#,#usernamehere";
pattern.Match(s).Groups["name"].Value; //usernamehere
Using Linq:
using System.Linq;
var input = #"24;#usernamehere,#AWRFR\user,#,#,#usernamehere";
You can split it with a single line:
var x = input.Split('#').Where(e => e.Contains(',')).Select(e => e.Split(',').First());
which is the same as:
var x = from e in input.Split('#')
where e.Contains(',')
select e.Split(',').First();
in both cases the result would be:
x = {"usernamehere", "AWRFR\user", "", ""}
Which is exactly an array with all substrings enclosed by # and ,.
Then if you want the first element just add .First() or do:
x.First();
You need to find the first index of '#' & ','. Then use substring method to get your required trimmed string. Read this for more details on substring method
string s = #"24;#usernamehere,#AWRFR\user,#,#,#usernamehere";
string finalString = s.Substring(s.IndexOf('#') + 1, s.IndexOf(',') - s.IndexOf('#') - 1);
Not exactly the way you asked for it, but should do what you want...
string input = #"24;#usernamehere,#AWRFR\user,#,#,#usernamehere";
string username = input.Substring(input.LastIndexOf("#") + 1);
If you wanted you could get the position of the first # and the ,
int hashPosition = input.IndexOf("#") + 1;
int commaPosition = input.IndexOf(",");
string username = input.Substring(hashPosition, commaPosition - hashPosition));