How do I get two numbers between two words (C#) [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a string "Building1Floor2" and it's always in that format, how do I cleanly get the building number (e.g. 1) and floor number. I'm thinking I need a regex, but not entirely sure that's the best way. I could just use the index if the format stays the same, but if I have have a high floor number e.g. 100 it will break.
P.S. I'm using C#.

Use a regex like this:
Building(\d+)Floor(\d+)

Regex would be an ok option here if "Building" and "Floor" could change. e.g.: "Floor1Room23"
You could use "[A-Za-z]+([0-9]{1,})[A-Za-z]+([0-9]{1,})"
With those groupings, $1 would now be the Building number, and $2 would be Floor.
If "Building" and "Floor" never changed, however, then regex might be overkill.. you could use a string split

Find the index of the "F" and substring on that.
int first = str.IndexOf("F") ;
String building = str.substring(1, first);

Related

How to find __tokens__ in a string without using RegEx in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have the following:
The __animal__ jumped over the __object__
I'd like to write a concise method in C# that returns animal and object. How can this be done without RegEx? A dirty solution would be to iterate through the characters one by one until we find a __ and then build a string until we find the closing __ - but I'm looking for a more elegant approach.
You are going to have to iterate in some form - if you don't want to use regex.
string text = "The __animal__ jumped over the __object__";
List<string> words = text.Split(' ').ToList();
words = words.Where(x => x.StartsWith("__") && x.EndsWith("__")).ToList();
You can use extensions of IEnumerable.to search, like above. but the framework is technically iterating.

C# Check if a string is a Sentence [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 7 years ago.
Improve this question
Basically I want to check if a String is a Sentence ("Hello, I am Me!") or Symbol Spam ("HH,,,{''{"), without using the number of symbols as a factor as much as possible. Right now it just detects based on a counter of symbols, but when someone says something with lots of punctuation, they get kicked.
Help?
If the number of symbols in the text is not sufficient, and you don't want to use something too fancy (or bought) could I suggest implementing one or more of these further steps (of increasing difficulty):
Make a count of all A-Za-z and space characters in the string and make a ratio of this to the count of symbols - so if they write a sentence then !!!!!!!!!!!!! at the end it still doesn't snag as the ratio is high enough.
If this still isn't discerning enough, add a further check if you pass item 1...
Count numbers of consecutive A-Za-z characters in the string - work out the average length of these 'words' - if the average is too short then it is probably spam.
These can be done in RegEx reasonably easily - If you want more sophistication then you have to use something written by someone else that has much more developed statistical methods (or start reading lexographical university papers that are beyond me!)

Regex to get last 4 characters before first occurrence of "/" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Below are the test input . The result I want is AAA- and AB-- respectively. The input string has no specific pattern. So first occurrence of / and the 4 character before that is the result I want to have.
Test line abc (r);AAA-/2010/001
Test line abc (r);AB--/2010/001
Like that? (EDIT: in .NET regex patterns, / does not need escaping)
(.{4})/
Consider sites like https://regex101.com for future needs)

RegEX : Find match based on condition [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am new to RegEx and looking for a solution to this condition. I am writing my code in C# and don't want to use any STRING functions.
Below is a sample string that I get:
610WBDFGFGM0122544
Now the condition I need to check is,
If string starts with "6" && 6th character is "D"
Can anyone tell me how to write RegEx for the above condition?
You can use this regex:
^6.{4}D
Here, ^ is the start of the string, .{4} means any four characters.
Online demo

Replace word in string but ignore accented characters [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
If the input string is
Cat fish bannedword bread bánnedword mouse bãnnedword
It should output
Cat fish bread mouse
What would be the best way to do this without slowing down the performance?
There are number of ways you can use but non of them (at least as far as I know) will work without certain performance cost.
The most obvious way is to remove the accented characters first and then use simple string.Replace(). As for removing accented characters this or this stackoverflow questions should help you.
Other approach could be splitting the string into an array of strings (each string being separate word) and then removing each word that equals the 'bannedword' using a parameter that makes Equals() method ignore accents.
Something like:
string[] splittedInput = input.Split(' ');
StringBuilder output = new StringBuilder();
foreach(string word in splittedInput)
{
if(string.Compare(word, bannedWord, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == false)
{
output.Append(word);
}
}
string s_output = output.ToString();
//I've not tested it in Visual Studio so there might be mistakes... (A LINQ could also simplify it (and potentially enable pluralization)).
And finally, it should be possible to come up with a clever regex solution (probably the fastest way) but not being an expert on regex I can't help you with that (this might point you in the right direction (if you know at least something about regexes)).

Categories