How can I return a string between two other strings in C#? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string for a page source already created. I need to grab a few lines of text from the string. The string I need is between two other strings. These two strings are "keywords": and ", "
How would I search for a string that has a colon after the quotations such as "keywords":
?
Would I use regex?
Thank you.

In your case, regex is too powerful to use it with such a problem. Just use string.IndexOf() and string.Substring(). Get a position of the word, get a position of the closest comma - there is an overload for this in IndexOf that let you specify starting position of searching.
Here is a code snippet, it is more explaining then I could do it in words.
var text = "\"keywords\":some text you want,and a text you do not want";
var searchFor = "\"keywords\":";
int firstIndex = text.IndexOf(searchFor);
int secondIndex = text.IndexOf(",", firstIndex);
var result = text.Substring(firstIndex + searchFor.Length, secondIndex - searchFor.Length);

The following Regex will match everything between "keywords" and ",":
Regex r = new Regex("keywords:(.*),");
Match m = r.Match(yourStringHere);
foreach(Group g in m.Groups) {
// do your work here
}

You can try like this, without using Regex
string str = "This is an example string and my data is here";
string first = "keywords:";
string second = ",";
int Start, End;
if (str.Contains(first) && str.Contains(second))
{
Start = str.IndexOf(first, 0) + first.Length;
End = str.IndexOf(second, Start);
return str.Substring(Start, End - Start);
}
else
{
return "";
}

This ought to work across multiple lines.
string input = #"blah blah blah ""keywords"":this is " + Environment.NewLine + "what you want right?, more blah...";
string pattern = #"""keywords"":(.*),";
Match match = Regex.Match(input, pattern, RegexOptions.Singleline);
if (match.Success)
{
string stuff = match.Groups[1].Value;
}

Related

string.IndexOf ignoring escape sequences [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to extract the CN of an LDAP DN string.
Here's an example string that illustrates the problem
var dn = #"CN=Firstname Lastname\, Organization,OU=some ou,DC=company,DC=com";
What I want is the position of the first non escaped ',' character, which is at position 32.
var pos = dn.IndexOf(',');
returns the first comma, regardless of escaping or not. Now can I bring IndexOf to skip the escaped comma in the string?
Assuming that \ should be escaped by itself: \\ to put just \ you can implement a simple
finite state machine
private static int IndexOfUnescaped(string source,
char toFind,
char escapement = '\\') {
if (string.IsNullOrEmpty(source))
return -1;
for (int i = 0; i < source.Length; ++i)
if (source[i] == escapement)
i += 1; // <- skip the next (escaped) character
else if (source[i] == toFind)
return i;
return -1;
}
...
var dn = #"CN=Firstname Lastname\, Organization,OU=some ou,DC=company,DC=com";
var pos = IndexOfUnescaped(dn, ',');
You can use Regex:
string s = #"CN=Firstname Lastname\, Organization,OU=some ou,DC=company,DC=com";
Regex regex = new Regex("(?<!\\\\),", RegexOptions.Compiled);
int firstMatch = regex.Matches(s).FirstOrDefault()?.Index ?? -1;
Demo: https://regex101.com/r/Jxco8K/1
It's using a negative lookbehind, so check all commas and look if it's not preceeded by a backslash.
Colleague of mine whipped up this regex. Not entirely the question, but since I wanted the position to then use SubString it also does the trick.
var CnRegex = new Regex(#"([a-zA-Z_]*)=((?:[^\\,}]|\\.)*)");
var match = CnRegex.Match(input);
if (match.Success)
return match.Value;
return null;
I feared it would come down to a Regex, as in Tim's solution, or 'brute force' as with Dmitry's solution.

How to get all matches using regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i need to get all matches in a given word using regex(i.e., all combinations)
Content:
ABC
From this i need to get AB and BC when i give some pattern like [A-Z][A-Z].
Now it is giving only "AB" as matched pattern.
Thanks in advance
int i = 0;
List<Match> matches = new List<Match>();
while(i < input.Length){
Match m = Regex.Match(input.Substring(i),"[A-Z]{2}");
if(m.Success){
matches.Add(m);
i += m.Index+1;
}else break;
}
You can also implement it to support lazy matching like this:
public static IEnumerable<Match> Matches(string input, string pattern) {
int i = 0;
while (i < input.Length){
Match m = Regex.Match(input.Substring(i), "[A-Z]{2}");
if (m.Success) {
yield return m;
i += m.Index + 1;
}
else yield break;
}
}
//Use it
var matches = Matches(input, "[A-Z]{2}");
You can use a lookahead so that you don't consume the matches:
(?=([A-Z]{2}))
ideone demo.
.NET supports capture groups in lookarounds
var result=Regex.Matches(input,"(?=(..))")
.Cast<Match>()
.Select(x=>x.Groups[1].Value);
int i = 0;
List<Match> matches = new List<Match>();
while(i < input.Length){
Match m = Regex.Match(input,"[A-Z][Z-A]");
if(m.Success){
matches.Add(i++);
i = m.Index ++ 1;
}
}

Characters between two exact characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let me show you what I want to do...
For example, I have this as an input
......1.......1.................
and what I want to do is
.......1111111..................
So I want to fill the space between the two ones with ones...
Also this should be able to be done too:
......11.....1..................
........11111...................
So I want just the inside...
Any C# help you can give?
This can be solved much easier without requiring a regular expression: You just want to "invert" the area of the string delimited by the first and last occurrence of a "1".
Here's an example solution:
string input = "..........1............1...";
int start = input.IndexOf('1');
int end = input.LastIndexOf('1');
char[] content = input.ToCharArray();
for (int i = start; i <= end; i++)
{
content[i] = content[i] == '1' ? '.' : '1'; //invert
}
string output = new string(content);
a regex way:
with multiline mode:
pattern: (?>(?<=^\.*)|\G)1(?=1*(\.))|\G(?<!^)\.(?=\.*(1))
replacement: $1$2
example:
string pattern = #"(?>(?<=^\.*)|\G)1(?=1*(\.))|\G(?<!^)\.(?=\.*(1))";
string input = #"......1.......1.................
......11.....1..................
......11111111..................";
string replacement = "$1$2";
Regex rgx = new Regex(pattern, RegexOptions.Multiline);
string result = rgx.Replace(input, replacement);

How to find the capital substring of a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to find the capitalized portion of a string, to then insert two characters that represent the Double Capital sign in the Braille language. My intention for doing this is to design a translator that can translate from regular text to Braille.
I'll give an example belo.
English String: My variable is of type IEnumerable.
Braille: ,My variable is of type ,,IE-numberable.
I also want the dash in IE-numerable to only break words that have upper and lower case, but not in front of punctuation marks, white spaces, numbers or other symbols.
Thanks a lot in advance for your answers.
I had never heard of a "Double Capital" sign, so I read up on it here. From what I can tell, this should suit your needs.
You can use this to find any sequence of two or more uppercase (majuscule) Latin letters or hyphens in your string:
var matches = Regex.Matches(input, "[A-Z-]{2,}");
You can use this to insert the double-capital sign:
var result = Regex.Replace(input, "[A-Z-]{2,}", ",,$0");
For example:
var input = "this is a TEST";
var result = Regex.Replace(input, "[A-Z-]{2,}", ",,$0"); // this is a ,,TEST
You can use this to hand single and double capitals:
var input = "McGRAW-HILL";
var result = Regex.Replace(input, "[A-Z-]([A-Z-]+)?",
m => (m.Groups[1].Success ? ",," : ",") + m.Value); // ,Mc,,GRAW-HILL
You can find them with a simple regex:
using System.Text.RegularExpressions;
// ..snip..
Regex r = new Regex("[A-Z]"); // This will capture only upper case characters
Match m = r.Match(input, 0);
The variable m of type System.Text.RegularExpressions.Match will contain a collection of captures. If only the first match matters, you can check its Index property directly.
Now you can insert the characters you want in that position, using String.Insert:
input = input.Insert(m.Index, doubleCapitalSign);
this code can solve your problema
string x = "abcdEFghijkl";
string capitalized = string.Empty;
for (int i = 0; i < x.Length; i++)
{
if (x[i].ToString() == x[i].ToString().ToUpper())
capitalized += x[i];
}
Have you tried using the method Char.IsUpper method
http://msdn.microsoft.com/en-us/library/9s91f3by.aspx
This is another similar question that uses that method to solve a similar problem
Get the Index of Upper Case letter from a String
If you just want to find the first index of an uppercase letter:
var firstUpperCharIndex = text // <-- a string
.Select((chr, index) => new { chr, index })
.FirstOrDefault(x => Char.IsUpper(x.chr));
if (firstUpperCharIndex != null)
{
text = text.Insert(firstUpperCharIndex.index, ",,");
}
Not sure if this is what you are going for?
var inputString = string.Empty; //Your input string here
var output = new StringBuilder();
foreach (var c in inputString.ToCharArray())
{
if (char.IsUpper(c))
{
output.AppendFormat("_{0}_", c);
}
else
{
output.Append(c);
}
}
This will loop through each character in the inputString if the characater is upper it inserts a _ before and after (replace that with your desired braille characters) otherwise appends the character to the output.

How to remove extra hyphens from string in c#? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string in which spaces are replaced by hyphen i.e '-' if there multiple hyphens then I want to remove all but one from the string. Only hyphens must be removed; not numbers that are consecutive.
Eg: --11- must be -11- and not -1-
Eg: --12- o/p: -12-
Eg: -12-- o/p: -12-
using Linq or a string function in C#.
I have tried it using str = str.Remove(str.Length - 1);, but it only removes one character.
If you just want to collapse multiple consecutive - characters into one, you could easily do this using regex:
string output = Regex.Replace(input, #"\-+", "-");
try
string sample = "--12";
string Reqdoutput = sample.Replace("--", "-");
If you want to replace just the hyphen, you can do one of the things given in the other answers. For removing all double characters, you can do this:
String input = "------hello-----";
int i = 1;
while (i < input.Length)
{
if (input[i] == input[i - 1])
{
input = input.Remove(i, 1);
}
else
{
i++;
}
}
Console.WriteLine(input); // Will give "-helo-"
Why not just do :
yourString = yourString.Replace("--", "-");
Or did I understand the problem wrong ?

Categories