REGEX specific word from Name [closed] - c#

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 1 year ago.
Improve this question
I am trying to figure out regex for wrongly formated names where the user has entered in the name and the relationship as one value, for example
[Son of Joseph Joestar ] => s/o. Joseph Joestar
the problem is since there was no validation, the users entered different variations like
s/o , s/, s/Joseph... , etc
this is so far I have gotten
^(s\/o.)(S\/o.)(s\/o)(S\/o)(s\/)(S\/)\w+
the relation is at the beginning or at the start, and after that is the name
there are 3 more cases Daughter(D/o.), Wife(W/o.), Father(F/o.)
I want to know the corresponding REGEX to filter out the relation prefixes
Thank You In advance

Maybe start with something like that
string foo = "s/o. Joseph Joestar";
// Look (and capture) for SDWF followed by "/" and
// EITHER "o" and maybe "." and maybe a white space
// OR we look ahead (?= ) and see a Upper wordchar followeed by lower word chars.
// look ahead because we do not want to have anything to do with this when replacing
string bar = Regex.Replace(foo, #"^([sSdDwWfF])/(o\.?\s?|(?=[A-Z]\w+))", match =>
{
string relation = match.Groups[1].Value.ToUpper() switch
{
"S" => "Son of",
"D" => "Daughter of",
"F" => "Father of",
"W" => "Wife of",
_ => throw new Exception("Oops")
};
return $"{relation} ";
});

Related

A regex to match excel function name / replace excel function name to another function name [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I face the following problem....
I have for example one of those strings:
string formula1 = "=SUMME(A1:A6)"
string formula2 = "=RUNDEN(SUMME(A6);1)"
string formula3 = "=AUFRUNDEN(ABS(RUNDEN(SUMME(A1:A6);2));2)"
string formula4 = "=AUFRUNDEN(RUNDEN(ABS(RUNDEN(SUMME(A1:A6);2));2);2)"
these are Excel formulas in German language
but I really need the function names to be replaced to function names in English language
I have thought about creating a dictionary, consisting of German function names(Keys)and English function names(Values)
would it be appropriate to find the German function names via regex in order to replace them with English terms later on?
unfortunately my regex knowledge is not really good and it is hard for me to create a regex that would match this pattern. Or would you solve it differently?
hope that is so far understandable. Thx for helping
You can try
(?<name>[A-Za-z][A-Za-z0-9]*)\s*\(
pattern; so function
Must start from A..Z or a..z
Can contain A..Z or a..z or 0..9 charactes
Must end with zero or more whitespaces and (
Code:
private static Dictionary<string, string> subs =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
//TODO: add {Deutsch, English}, pairs here
{ "RUNDEN", "ROUND" },
{ "ABS", "ABS" },
{ "SUMME", "SUM" },
{ "AUFRUNDEN", "ROUNDUP"},
};
private static string ConvertExcel(string formula) {
return Regex.Replace(
formula,
#"(?<name>[A-Za-z][A-Za-z0-9]*)\s*\(",
m => subs.TryGetValue(m.Groups["name"].Value, out var newValue)
? newValue + "("
: m.Value); // do nothing, if translation is not found
}
Demo:
string[] tests = new string[] {
"=SUMME(A1:A6)",
"=RUNDEN(SUMME(A6);1)",
"=AUFRUNDEN(ABS(RUNDEN(SUMME(A1:A6);2));2)",
"=AUFRUNDEN(RUNDEN(ABS(RUNDEN(SUMME(A1:A6);2));2);2)",
};
var result = string.Join(Environment.NewLine, tests
.Select(test => $"{test,-52} <=> {ConvertExcel(test)}"));
Console.Write(result);
Outcome:
=SUMME(A1:A6) <=> =SUM(A1:A6)
=RUNDEN(SUMME(A6);1) <=> =ROUND(SUM(A6);1)
=AUFRUNDEN(ABS(RUNDEN(SUMME(A1:A6);2));2) <=> =ROUNDUP(ABS(ROUND(SUM(A1:A6);2));2)
=AUFRUNDEN(RUNDEN(ABS(RUNDEN(SUMME(A1:A6);2));2);2) <=> =ROUNDUP(ROUND(ABS(ROUND(SUM(A1:A6);2));2);2)

how do you take a string then organize the words in it with how many times the word occurs in c#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
ok I have no idea on how to do this and i have tried looking up how to do this but nothing good came up so ill ask it here. So what i am trying to do is:
string input = TextEditor.text; <-- this is in windows form application and
The "TextEditor" is the textbox for input
i want to take the string (which is input from the texct box) then split it so each word is on every other line like so:
if input = "hi my name is is"
out put should be:
hi: 1
my: 1
name: 1
is: 2 <-- if the word is said it shouldn't be repeated.
could someone please help me? I am a true newbie and i am completely lost. I don't have any code yet because I DONT KNOW HOW TO DO THIS!
Use Linq GroupBy and Count:
string inputText = "hi my name is is";
var words = inputText.Split(' ').ToList();
var wordGroups = words.GroupBy(w => w).Select(grp => new {
Word = grp.Key,
Count = grp.Count()
});
string outputText = string.Join("\n", wordGroups.Select(g => string.Format("{0}:\t{1}", g.Word, g.Count)));
/*
hi: 1
my: 1
name: 1
is: 2
*/
Split the input string into an array, then use that to build a dictionary. If the word is already in the dictionary, increment it. Otherwise add it with an initial value of 1.
string input = TextEditor.text;
string[] inputWords = input.Split(' ');
Dictionary<string, int> wordCounts = new Dictionary<string, int>();
foreach (string word in inputWords)
{
if (wordCounts.ContainsKey(word))
{
wordCounts[word]++;
}
else
{
wordCounts.Add(word, 1);
}
}

Extracting Address' from police reports [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm currently working on a program that would be searching through medium-large strings and pulling out addresses so they can then be geocoded.
An example of what I had was:
private void cardCheck()
{
cardCount = mobjEntity.CardCount;
for (int i = 0; i < cardCount; i++)
{
card = mobjEntity.Card[i];
if (card.Text.Contains(" STREET ") ||
card.Text.Contains(" Street") ||
card.Text.Contains(" street") ||
card.Text.Contains(" ST ") ||
card.Text.Contains(" St ") ||
card.Text.Contains(" st "))
{
}
}
}
I'm not very good at regex and I was hoping one of you regex wiz' could show me a useful link for testing/learning regex.
I have written what I have above for every street type and it's very tedious and I still don't even know what to do from there.
This is an Ideal Input Output:
Input:
On Friday, April 9, 2010, at 9:45 a.m., I, Officer Janice Ruiz, was dispatched to 2170 Powell Street to investigate a burglary. I met with Frank Gaines, the homeowner who had reported the burglary.
Output:
2170 Powell Street
Have you tried using a regular expression to search the text? A quick Google search returns several RE's that may work for you. Here is one example:
\d{1,3}.?\d{0,3}\s[a-zA-Z]{2,30}\s[a-zA-Z]{2,15}
Here is a proof of concept: https://regex101.com/r/dH3jJ8/1

Extract part of the string based on delimiters [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 8 years ago.
Improve this question
I need help to develop a logic that will extract part of the string when the user enter a "delimiter"
For example:
string data = "|cBlue|pDaisy|pRose|mTomato|mWheat|pCabbage|p100 units|d19.0";
string userInput = //User enter input
So, if the user enters "|c", it should return "Blue"
If the user enters "|m", it should return "Tomato", "Wheat" as 2 strings.
If the user enters "|p", it should return "Daisy", "Rose", "Cabbage" and "100 units" as 4 different strings.
If the user enters something that is not exist, say for example, |z, it will return nothing or an empty string "".
Note: This is just a sample data, the actual data consists of |a - |z, |A - |Z
Start with string.Split() to tokenize the string.
Then iterate over each token. Pull out its first character and construct a Dictionary<char, string> using the first character as a key and the remainder as a value.
Then simply do a dictionary lookup on the desired character to find the relevant token.
First get the input,parse it.Use Split method then filter the words by first letter,Something like this:
var userInput = Console.ReadLine();
if(userInput.Length == 2)
{
var words = data.Split(userInput[0]).Where(x => x.StartsWith(userInput[1].ToString()));
if(words.Any())
{
var result = words.Select(x => x.Substring(1)).ToList();
}
else
{
// no word found
}
}

How do I capture integers from a string in C#? [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
In this project I need to separate the Booking ID from the string below into a separate integer.
Booking ID: 27620
Booked for Paitient(ID): 003
Booked Staff ID: 001
Booked Room: b001
Booked Equipment: Adam
How do I do this in C#?
To clarify: The code above is the whole string, the first line is where the booking ID is contained. I need help finding a way to separate the ID from the string.
I have tried Regex but to no prevail. I cannot get my head around the string formatting.
The ID can be any length.
Regex can handle what you are looking for.
string text = "Booking ID: 27620 ...";
foreach (string line in new LineReader(() => new StringReader(text))
{
string result = Regex.Replace(line, #"[^\d]", "");
Console.WriteLine(result); // >> 27620
}
The regex "[^\d]" says:
[] Capture
^\d Everything that is not (^) a digit (\d)
Then the Replace replaces it all with an empty string.
This would be most valuble if you were looping through each line of your file.
If you needed to keep the field value, then running a simple split would be the better way to go.
string input = "Booking ID: 27620";
Dictionary<string, string> dict =
new Dictionary<string, string>{key = input.Split(':')[0],
value = input.Split(':')[1]};

Categories