How can I create a regular expression that will match numbers of length 2 from a given string.
Example input:
givenpercentage#60or•70and 8090
Desired output:
60 70 80 90
Try this:
string x = "givenpercentage#60or•70and 8090";
Regex r = new Regex(#"\d{2}");
foreach(Match m in r.Matches(x))
{
string temp = m.Value;
//Do something
}
\d -> only numbers
{2} -> 2 numbers only
Output will be:
60 70 80 90
Related
I am new to C# and trying to lean how to filter data that I read from a file. I have a file that I read from that has data similer to the follwoing:
3 286 858 95.333 0.406 0.427 87.00 348 366 4 b
9 23 207 2.556 0.300 1.00 1.51 62 207 41 a
9 37 333 4.111 0.390 0.811 2.03 130 270 64 a
10 21 210 2.100 0.348 0.757 3.17 73 159 23 a
9 79 711 8.778 0.343 0.899 2.20 244 639 111 a
10 66 660 6.600 0.324 0.780 2.25 214 515 95 a
When I read these data, some of them have Carriage return Or Line Feed characters hidden in them. Can you please tell me if there is a way to remove them. For example, one of my variable may hold the the following value due to a newline character in them:
mystringval = "9
"
I want this mystringval variable to be converted back to
mystringval = "9"
If you want to get rid of all special characters, you can learn regular expressions and use Regex.Replace.
var value = "&*^)#abcd.";
var filtered = System.Text.RegularExpressions.Regex.Replace(value, #"[^\w]", "");
REGEXPLANATION
the # before the string means that you're using a literal string and c# escape sequences don't work, leaving only the regex escape sequences
[^abc] matches all characters that are not a, b, or c(to replace them with empty space)
\w is a special regex code that means a letter, number, or underscore
you can also use #"[^A-Za-z0-9\.]" which will filter letters, numbers and decimal. See http://rubular.com/ for more details.
As well as using RegEx, you can use LINQ to do something like
var goodCharacters = input
.Replace("\r", " ")
.Replace("\n", " ")
.Where(c => char.IsLetterOrDigit(c) || c == ' ' || c == '.')
.ToArray();
var result = new string(goodCharacters).Trim();
The first two Replace calls will guard against having a number at the end of one line and a number at the start of the next, e.g. "123\r\n987" would otherwise be "123987", whereas I assume you want "123 987".
Try my sample here on ideone.com.
Input 1
string str=" 1 KAUSHAL DUTTA 46 Female WL 19 WL 2";
Input 2
string str1= "1 AYAN PAL 38 Male CNF S5 49 (LB) CNF S5 49 (LB)";
i have two different types of string if user enter string str then the output should be (WL 2) & if user enter string str1 then the output should be(CNF S5 49 (LB))
all the values are dynamic except(WL (number)) (CNF (1 alphabet 1
or 2 number) number (LB))
If you frame your input string with some delimiter, then you can easily split the string and you can store it in some array and proceed.
For example, Frame your string as
string str="1#KAUSHAL DUTTA#46#Female#WL 19#WL 2";
After this split the string like
string[] str1 = str.Split('#');
From str1 array, you can take last value str1[5]
You can use Regex:
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
//This is the pattern for the first case WL followed by a one or more (+) digits (\d)
//followed by any number of characters (.*)
//the parenthesis is to us to be able to group what is inside, for further processing
string pattern1 = #"WL \d+ (.*)";
//Pattern for the second match: CNF followed by a letter (\w) followed by one or two ({1,2})
//digits (\d) followed by one or more (+) digits (\d), followed by (LB) "\(LB\)"
//the backslach is to get the litteral parenthesis
//followed by any number of characters (.*)
//the parenthesis is to us to be able to group what is inside, for further processing
string pattern2 = #"CNF \w\d{1,2} \d+ \(LB\) (.*)";
string result="";
if (Regex.IsMatch(inputString, pattern1))
{
//Groups[0] is the entire match, Groups[1] is the content of the first parenthesis
result = Regex.Match(inputString, pattern1).Groups[1].Value;
}
else if (Regex.IsMatch(inputString, pattern2))
{
//Groups[0] is the entire match, Groups[1] is the content of the first parenthesis
result = Regex.Match(inputString, pattern2).Groups[1].Value;
}
I used C# and I would like to match 3 doubles seperated by comma(maybe yes\not) or\and by spaces(maybe yes\not and one or more)
I did:
Regex regex = new Regex(#"\d+[,|\s*]\d+[,|\s*]\d+");
Match match = regex.Match(mystr.Text);
Issue is that the below text isn't match:
33 44 55 (after 33 and after 44 there is two spaces)
Also, the match didn't catch:
33, 44, 55 (after comma there is also one space)
Any advice?
Thanks!
The regex inside brackets are wrong, you may try with this:
\d+[,\s]*\d+[,\s]*\d+
Would something like this function for you?
(\d{2},?\s+){2}\d{2}
It matches 2 digits then zero or one comma, with multiple spaces, and it does it twice and ending with 2 digits?
Here's a simple solution for you
string nmbrs = "33 44";
string numberPattern = #"\d+(?=[,\s]*\d+[,\s]*)\d+";
var matches = Regex.Matches(nmbrs, numberPattern);
List<int> numbersList = new List<int>();
foreach (var match in matches)
{
numbersList.Add(int.Parse(match.ToString()));
}
I have string like
1 69 / EMP1094467 EMP1094467 : 2 69 / ScreenLysP
here the numeric characters should be replace with empty characters, Llike:
/ EMP1094467
I tried like this
var output = Regex.Replace(input, #"[\d-]", string.Empty);
which produced the following result:
/ EMP
Please suggest a better solution.
You can try using word boundaries:
var input = "1 69 / EMP1094467 EMP1094467 : 2 69 / ScreenLysP ";
var output = Regex.Replace(input, #"\b[\d]+\b", string.Empty);
string.Substring seems fitting here:
var str = "1 69 / EMP1094467";
var result = str.Substring(str.IndexOf("/")); // "/ EMP1094467"
I need help in removing letters but not words from an incoming data string. Like the following,
String A = "1 2 3A 4 5C 6 ABCD EFGH 7 8D 9";
to
String A = "1 2 3 4 5 6 ABCD EFGH 7 8 9";
You need to match a letter and ensure that there is no letter before and after. So match
(?<!\p{L})\p{L}(?!\p{L})
and replace with an empty string.
Look around assertions on regular-expresssion.info
Unicode properties on regular-expresssion.info
In C#:
string s = "1 2 3A 4 5C 6 ABCD EFGH 7 8D 9";
string result = Regex.Replace(s, #"(?<!\p{L}) # Negative lookbehind assertion to ensure not a letter before
\p{L} # Unicode property, matches a letter in any language
(?!\p{L}) # Negative lookahead assertion to ensure not a letter following
", String.Empty, RegexOptions.IgnorePatternWhitespace);
The "obligatory" Linq approach:
string[] words = A.Split();
string result = string.Join(" ",
words.Select(w => w.Any(c => Char.IsDigit(c)) ?
new string(w.Where(c => Char.IsDigit(c)).ToArray()) : w));
This approach looks if each word contains a digit. Then it filters out the non-digit chars and creates a new string from the result. Otherwise it just takes the word.
And here comes the old school:
Dim A As String = "1 2 3A 4 5C 6 ABCD EFGH 7 8D 9"
Dim B As String = "1 2 3 4 5 6 ABCD EFGH 7 8 9"
Dim sb As New StringBuilder
Dim letterCount As Integer = 0
For i = 0 To A.Length - 1
Dim ch As Char = CStr(A(i)).ToLower
If ch >= "a" And ch <= "z" Then
letterCount += 1
Else
If letterCount > 1 Then sb.Append(A.Substring(i - letterCount, letterCount))
letterCount = 0
sb.Append(A(i))
End If
Next
Debug.WriteLine(B = sb.ToString) 'prints True