display some part of string on a label - c#

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;
}

Related

How to match different scenarios with regex in c# and groups

I want to match these different scenarios with a regex pattern. Mainly delimiter is #:
1234-1111-234.011#333 => [id = 1234-1111-234.011 and code =333]
whatever text before 1234-1111-234.011#333 => [textb=whatever text before, id = 1234-1111-234.011 , code =333, texta="]
1234-1111-234.011#333 whatever text after => [ textb="" id = 1234-1111-234.011 ,code =333 , texta=whatever text after]
Text can be both at the beginning or the end
In every case code can contain also a postfix letter W like 1234-1111-234.011#333W => code=333E
textb = text with length maximum 15 characters. Only letters and
numbers.
id = 17 character long with this format XXXX-XXXX-XXX.XXX code - 3
or 4 character long based on W letter is presenting or not
texta = text with length maximum 15 characters. Only letters and
numbers.
I am trying to match these scenarios with this piece of code and groups
pattern ="
?(<textb>[\w\s]{15})#
?(<id>[\d\s]{17,17})#
(?<code>([A-Z]{0,1}\d{2,3}))
(?<wo>[W]{1})
?(<texta>[\w\s]{15})"
and
var textb = Regex.Match(mytext, pattern).Groups["textb"].Value;
var id= Regex.Match(mytext, pattern).Groups["id"].Value;
var code= Regex.Match(mytext, pattern).Groups["code"].Value;
var wo= Regex.Match(mytext, pattern).Groups["wo"].Value;
var texta= Regex.Match(mytext, pattern).Groups["texta"].Value;
A full example is "This is before text 234-1111-234.011#333E This is next text"
Not matching at all.
You could do it with one regular expression and then use Groups to get the parts you need.
void Main()
{
var input = "Before text 1234-1111-234.011#333E After text";
var pattern = #"(?<btext>[\w ]{0,15})(?<id>[\d\-\.]{17})#(?<code>[\d]{2,3})(?<wo>[A-Z]?)(?<atext>[\w ]{0,15})";
var matches = Regex.Match(input, pattern);
var btext = matches.Groups["btext"];
var wo = matches.Groups["wo"];
Console.WriteLine(btext.Value);
Console.WriteLine(wo.Value);
// etc.
}
(?<btext>[\w ]{0,15}) // Match letters, numbers and spaces, minimum 0 chars, maximum 15 chars
(?<id>[\d\-\.]{17}) // match numbers, '-' and '.'. Must be 17 chars
# // Match pound sign
(?<code>[\d]{2,3}) // Match numbers 2 or 3 chars long
(?<wo>[A-Z]?) // Match optional letter after code
(?<atext>[\w ]{0,15}) // Match letters, numbers and spaces, minimum 0 chars, maximum 15 chars

c# Regex - Only get numbers and whitespaces in one string and only text and whitespaces in another

How do I only get numbers and include whitespaces in one string and only text and white spaces in another?
Iv'e tried this:
string value1 = "123 45 New York";
string result1 = Regex.Match(value1, #"^[\w\s]*$").Value;
string value2 = "123 45 New York";
string result2 = Regex.Match(value2, #"^[\w\s]*$").Value;
result1 need to be "123 45"
result2 need to be " New York"
Try next code:
string value1 = "123 45 New York";
string digitsAndSpaces = Regex.Match(value1, #"([0-9 ]+)").Value;
string value2 = "123 45 New York";
string lettersAndSpaces = Regex.Match(value2, #"([A-Za-z ])+([A-Za-z ]+)").Value;
Update:
How do I allow charachters like å ä ö in result from value2?
string value3 = "å ä ö";
string speclettersAndSpaces = Regex.Match(value3, #"([a-zÀ-ÿ ])+([a-zÀ-ÿ ]+)").Value;
The fallowing regex will allow only digits and spaces between them, the same goes with characters.
Regex: (?:\d[0-9 ]*\d)|(?:[A-Za-z][A-Za-z ]*[A-Za-z])
Details:
(?:) Non-capturing group
\d matches a digit (equal to [0-9])
[] Match a single character present in the list
* Matches between zero and unlimited times
| or
Output:
Match 1
Full match 0-6 `123 45`
Match 2
Full match 7-15 `New York`
Regex demo

C# Regex to match all occurrences of a pattern and replace with empty string

I am trying to match a pattern <two alpha chars>single space<two digits>single space<two digits>and remove in all occurrences in a string.
var myRegex = #"(?:^|[\s]|[, ]|[.]|[\n]|[\t])([A-Za-z]{2}\s[0-9]{2}\s[0-9]{2})($|[,]|[.]|[\s]|[\n]|[\t])";
string myString = "this 02 34, HU 23 76 , hh 76 745 1.HO 12 33. HO 34 56";
var matches = Regex.Matches(myString, myRegex);
foreach (Match match in matches)
{
myString = myString.Replace(match.Value, "");
}
In above variable myString "this 02 34" will not match as there is no space or period or comma or new line or tab. This is expected behavior.
But "HO 34 56" is not matching as it is not ending with space or period or comma or new line or tab. How can I include this in the match and not have a match for "hh 76 745"
After executing above code, I expect myString variable to have "this 02 34, , hh 76 745 1.. "
Use this regex with word boundaries:
\b[A-Za-z]{2}\s[0-9]{2}\s[0-9]{2}\b
See the regex demo
Details:
\b - a leading word boundary
[A-Za-z]{2} - 2 alpha
\s - a whitespace
[0-9]{2} - 2 digits
\s - a whitespace
[0-9]{2} - 2 digits
\b - a trailing word boundary.
If you need to say "not preceded with alpha" replace the first \b with (?<![a-zA-Z]) and if you want to say "not followed with digit" replace the last \b with (?!\d). That is, use lookarounds, that, like word boundaries, are zero-width assertions.
If you really after matching that chunk when it has leading or trailer with following space or period or comma or new line or tab or beginning of string or end of string, use
(?<=^|[\s,.])[A-Za-z]{2}\s[0-9]{2}\s[0-9]{2}(?=$|[\s,.])
See this demo

c# regex to parse columns in a txt file

I have a text file looks like this
FieldA FieldB FieldC FieldD FieldE
001 中文 15% 语言
002 法文 20 12% 外文
003 英文 21 外文
004 西班牙语 10% 外文
so basically I have the file read in and split into lines. Now I would like to use regex to split each line into fields. As you can see some fields in the column are actually empty, the fields may not in fixed width, but is separated by at least one white space. Some fields contains Chinese characters.
May I know how to do this? Thanks.
string s = "001 中文 15% 语言";
Match m = Regex.Match(s,
#"(?<A>\d*)\s*" + // Field A: any number of digits
#"(?<B>\p{L}*)\s*" + // Field B: any number of letters
#"(?<C>\d*)\s+" + // Field C: any number of digits
#"(?<D>(\d+%)?)\s*" + // Field D: one or more digits followed by '%', or nothing
#"(?<E>\p{L}*)"); // Field E: any number of letters
string fieldA = m.Groups["A"].Value; // "001"
string fieldB = m.Groups["B"].Value; // "中文"
string fieldC = m.Groups["C"].Value; // ""
string fieldD = m.Groups["D"].Value; // "15%"
string fieldE = m.Groups["E"].Value; // "语言"
All fields are optional. If a field is not present, it will be captured as an empty string, like in fieldC above.
/\s*(\d*)\s*([^\d\s]*)\s*(\d*)\s\s*(\d*%?)\s*([^\d\s]*)/
Here is a regex that will capture all of the content you want, use it on each line.
\s* //any number of whitespace
(\d*) //any number of digits
\s* //any number of whitespace
([^\d\s]*) //any number of characters that aren't whitespace or digits
\s* //any number of whitespace
(\d*)\s //any number of digits with a space after it
\s* //any number of whitespace
(\d*%?) //any number of digits with an optional %
\s* //any number of whitespace
([^\d\s]*) //any number of characters that aren't whitespace or digits

checking input for morse code converter

I want to check the input from the user to make sure that they only enter dots and dashes and any other letters or numbers will give back and error message. Also i wanted to allow the user to enter a space yet when i am converting how can i remove or ignore the white space?
string permutations;
string entered = "";
do
{
Console.WriteLine("Enter Morse Code: \n");
permutations = Console.ReadLine();
.
.
} while(entered.Length != 0);
Thanks!
string permutations = string.Empty;
Console.WriteLine("Enter Morse Code: \n");
permutations = Console.ReadLine(); // read the console
bool isValid = Regex.IsMatch(permutations, #"^[-. ]+$"); // true if it only contains whitespaces, dots or dashes
if (isValid) //if input is proper
{
permutations = permutations.Replace(" ",""); //remove whitespace from string
}
else //input is not proper
{
Console.WriteLine("Error: Only dot, dashes and spaces are allowed. \n"); //display error
}
Let's assume that you separate letters by a single space and words by two spaces. Then you can test if your string is well formatted by using a regular expression like this
bool ok = Regex.IsMatch(entered, #"^(\.|-)+(\ {1,2}(\.|-)+)*$");
Regular expression explained:
^ is the beginning of the string.
\.|- is a dot (escaped with \ as the dot has a special meaning within Regex) or (|) a minus sign.
+ means one or more repetitions of what's left to it (dot or minus).
\ {1,2} one or two spaces (they are followed by dots or minuses again (\.|-)+).
* repeats the space(s) followed by dots or minuses zero or more times.
$ is the end of the line.
You can split the string at the spaces with
string[] parts = input.Split();
Two spaces will create an empty entry. This allows you to detect word boundaries. E.g.
"–– ––– .–. ... . –.–. ––– –.. .".Split();
produces the following string array
{string[10]}
[0]: "––"
[1]: "–––"
[2]: ".–."
[3]: "..."
[4]: "."
[5]: ""
[6]: "–.–."
[7]: "–––"
[8]: "–.."
[9]: "."

Categories

Resources