I have a string in (horizontal) form 184.b189.a194.b199.d204.d209.b214.b219.d which i need to convert in (vertical) form
184.b
189.a
194.b
199.d
.......
I have tried Regex to find each alphabet using below regex expression so I could append line break <br /> after each alphabet in string. Expression works fine, I could not figure out how to append line break
var count = Regex.Matches(text, #"[a-zA-Z]");
You can try Regex.Replace: we replace each A..Za..z match with itself $0 followed by a new line
string source = "184.b189.a194.b199.d204.d209.b214.b219.d";
string result = Regex.Replace(source, "[A-Za-z]", $"$0{Environment.NewLine}");
Console.Write(result);
Outcome:
184.b
189.a
194.b
199.d
204.d
209.b
214.b
219.d
Same idea if you want to add <br />
string result = Regex.Replace(source, "[A-Za-z]", $"$0<br />");
Linq is an alternative:
string result = string.Concat(source
.Select(c => c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'
? c.ToString() + "<br />"
: c.ToString()));
You can use the regex (\d{3}\.[A-Za-z]) https://regex101.com/r/Z05cC4/1,
which is:
\d{3} matches a digit (equal to [0-9])
{3} Quantifier — Matches exactly 3 times
\. matches the character . literally (case sensitive)
Match a single character present in the list below [A-Za-z]
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
Then take the first group only.
public static class Program
{
private static void Main(string[] args)
{
string input = #"184.b189.a194.b199.d204.d209.b214.b219.d";
IEnumerable<string> capturedGroups = ExtractNumbers(input);
string res = string.Join(Environment.NewLine, capturedGroups);
Console.WriteLine(res);
}
static IEnumerable<string> ExtractNumbers(string Input)
{
string pattern = #"(\d{3}\.[A-Za-z])";
MatchCollection matches = Regex.Matches(Input, pattern, RegexOptions.Singleline);
foreach (Match match in matches)
yield return match.Groups[1].Value;
}
}
Outputting:
184.b
189.a
194.b
199.d
204.d
209.b
214.b
219.d
Related
I would like to transform this claim that I get "[\" 75 \ ", \" 91 \ "]" to (75,91)
I used regex.split but I don't know how to do it.
if (Zone != null)
{
filtrer.Append(" and depalcement in (");
foreach (string i in Zone)
{
var diviser=Regex.Split(i, #"\");
filtrer.Append(diviser);
}
}
No Regex, just JSON. Try this:
var content = "[\"75\",\"91\"]";
var list = System.Text.Json.JsonSerializer.Deserialize<List<string>>(content);
var numbers = list.Select(int.Parse);
foreach(var number in numbers)
{
Console.WriteLine(number);
}
For completeness; to get from "[\" 75 \", \" 91 \"]" to (75,91) using regex the code could be something like this:
var s2 = "(" + Regex.Replace(input: s, pattern: "[^0-9,]", "") + ")"; // or #"[^\d,]"
Pattern:
[...] - character set aka character class aka 'any of'
^ - negate
0-9 or \d - digit
, comma
together it's any character that isn't a digit or a comma
For example I have such string:
ex250-r-ninja-08-10r_
how could I change it to such string?
ex250 r ninja 08-10r_
as you can see I change all - to space, but didn't change it where I have XX-XX part... how could I do such string replacement in c# ? (also string could be different length)
I do so for -
string correctString = errString.Replace("-", " ");
but how to left - where number pattern XX-XX ?
You can use regular expressions to only perform substitutions in certain cases. In this case, you want to perform a substitution if either side of the dash is a non-digit. That's not quite as simple as it might be, but you can use:
string ReplaceSomeHyphens(string input)
{
string result = Regex.Replace(input, #"(\D)-", "${1} ");
result = Regex.Replace(result, #"-(\D)", " ${1}");
return result;
}
It's possible that there's a more cunning way to do this in a single regular expression, but I suspect that it would be more complicated too :)
A very uncool approach using a StringBuilder. It'll replace all - with space if the two characters before and the two characters behind are not digits.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
bool replace = false;
char c = text[i];
if (c == '-')
{
if (i < 2 || i >= text.Length - 2) replace = true;
else
{
bool leftDigit = text.Substring(i - 2, 2).All(Char.IsDigit);
bool rightDigit = text.Substring(i + 1, 2).All(Char.IsDigit);
replace = !leftDigit || !rightDigit;
}
}
if (replace)
sb.Append(' ');
else
sb.Append(c);
}
Since you say you won't have hyphens at the start of your string then you need to capture every occurrence of - that is preceded by a group of characters which contains at least one letter and zero or many numbers. To achieve this, use positive lookbehind in your regex.
string strRegex = #"(?<=[a-z]+[0-9]*)-";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
string strTargetString = #"ex250-r-ninja-08-10r_";
string strReplace = #" ";
return myRegex.Replace(strTargetString, strReplace);
Here are the results:
RegEx to get text form string between double quotes which contains at lest any one like
:=, &&, ||, ==, <, >, <=, >=, <>, &, +, -, *, /, ? .
Code:
script = "if(a<=b, strcat(\" userid <= 'tom' \",\" and test = 1\", \" and test >= 10 \"), nop());";
string tempScript = (script ?? "").Trim();
var functionMatches = Regex.Matches(tempScript, #"Some RegEx");
if (functionMatches != null && functionMatches.Count > 0)
{
foreach (Match fm in functionMatches)
{
Console.WriteLine(fm.Value);
}
}
Console.ReadLine();
Eg:
Input :-
"if(a<=b, strcat(" userid <= 'tom' "," and test = 1", " and test >= 10 "), nop());"
Output :-
1) " userid <= 'tom' "
2) " and test >= 10 "
Thanks.
Actually it appears to be not that simple...
I would recommend first looking for all strings between double quotes "..." then checking every string if it contains the special characters you provided. So it would be like this:
Regex:
(?<=\")[^,]+?(?=\")
^.+?(<=|>=|<>|\|\||:=|&&|==|<|>|&|\+|-|\*|/|\?).+?$
Code:
string pattern = "(?<=\")[^,]+?(?=\")";
string patternSpecial = #"^.+?(<=|>=|<>|\|\||:=|&&|==|<|>|&|\+|-|\*|/|\?).+?$";
string input = "if(a<=b, strcat(\" userid <= 'tom' \",\" and test = 1\", \" and test >= 10 \"), nop());";
foreach (Match m in Regex.Matches(input, pattern))
{
if (Regex.IsMatch(m.Value, patternSpecial))
{
Console.WriteLine(Regex.Match(m.Value, patternSpecial).Value);
}
}
Console.ReadLine();
Output:
userid <= 'tom'
and test >= 10
Using just one pattern and less C# code:
Regex:
\"([^"]+?(<=|>=|<>|\|\||:=|&&|==|<|>|&|\+|-|\*|/|\?).+?)\"
Code:
string pattern = "\"([^\"]"+#"+?(<=|>=|<>|\|\||:=|&&|==|<|>|&|\+|-|\*|/|\?).+?)"+"\"";
string input = "if(a<=b, strcat(\" userid <= 'tom' \",\" and test = 1\", \" and test >= 10 \"), nop());";
foreach (Match m in Regex.Matches(input, pattern))
{
Console.WriteLine(m.Groups[1]);
}
Console.ReadLine();
Output:
userid <= 'tom'
and test >= 10
I have a string which is //{characters}\n.
And I need a regular expression to extract the character in between // and \n.
Regular expressions are nice and all, but why not use Substring?
string input = "//{characters}\n";
string result = input.Split('\n')[0].Substring(2);
or
string result = input.Substring(2, input.Length - 3);
Using RegEx:
Regex g;
Match m;
g = new Regex("//(.*)\n"); // if you have just alphabet characters replace .* with \w*
m = g.Match(input);
if (m.Success == true)
output = m.Groups[1].Value;
This should work:
string s1 = "//{characters}\n";
string final = (s1.Replace("//", "").Replace("\n", ""));
How do I strip non alphanumeric characters from a string and loose spaces in C# with Replace?
I want to keep a-z, A-Z, 0-9 and nothing more (not even " " spaces).
"Hello there(hello#)".Replace(regex-i-want, "");
should give
"Hellotherehello"
I have tried "Hello there(hello#)".Replace(#"[^A-Za-z0-9 ]", ""); but the spaces remain.
In your regex, you have excluded the spaces from being matched (and you haven't used Regex.Replace() which I had overlooked completely...):
result = Regex.Replace("Hello there(hello#)", #"[^A-Za-z0-9]+", "");
should work. The + makes the regex a bit more efficient by matching more than one consecutive non-alphanumeric character at once instead of one by one.
If you want to keep non-ASCII letters/digits, too, use the following regex:
#"[^\p{L}\p{N}]+"
which leaves
BonjourmesélèvesGutenMorgenliebeSchüler
instead of
BonjourmeslvesGutenMorgenliebeSchler
You can use Linq to filter out required characters:
String source = "Hello there(hello#)";
// "Hellotherehello"
String result = new String(source
.Where(ch => Char.IsLetterOrDigit(ch))
.ToArray());
Or
String result = String.Concat(source
.Where(ch => Char.IsLetterOrDigit(ch)));
And so you have no need in regular expressions.
Or you can do this too:
public static string RemoveNonAlphanumeric(string text)
{
StringBuilder sb = new StringBuilder(text.Length);
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9')
sb.Append(text[i]);
}
return sb.ToString();
}
Usage:
string text = SomeClass.RemoveNonAlphanumeric("text LaLa (lol) á ñ $ 123 ٠١٢٣٤");
//text: textLaLalol123
The mistake made above was using Replace incorrectly (it doesn't take regex, thanks CodeInChaos).
The following code should do what was specified:
Regex reg = new Regex(#"[^\p{L}\p{N}]+");//Thanks to Tim Pietzcker for regex
string regexed = reg.Replace("Hello there(hello#)", "");
This gives:
regexed = "Hellotherehello"
And as a replace operation as an extension method:
public static class StringExtensions
{
public static string ReplaceNonAlphanumeric(this string text, char replaceChar)
{
StringBuilder result = new StringBuilder(text.Length);
foreach(char c in text)
{
if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9')
result.Append(c);
else
result.Append(replaceChar);
}
return result.ToString();
}
}
And test:
[TestFixture]
public sealed class StringExtensionsTests
{
[Test]
public void Test()
{
Assert.AreEqual("text_LaLa__lol________123______", "text LaLa (lol) á ñ $ 123 ٠١٢٣٤".ReplaceNonAlphanumeric('_'));
}
}
var text = "Hello there(hello#)";
var rgx = new Regex("[^a-zA-Z0-9]");
text = rgx.Replace(text, string.Empty);
Use following regex to strip those all characters from the string using Regex.Replace
([^A-Za-z0-9\s])
In .Net 4.0 you can use the IsNullOrWhitespace method of the String class to remove the so called white space characters. Please take a look here http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx
However as #CodeInChaos pointed there are plenty of characters which could be considered as letters and numbers. You can use a regular expression if you only want to find A-Za-z0-9.