If Statement not Working right [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 6 years ago.
Improve this question
I need some help with some operators in the If statement...
I am running a code like this
if (statement.Contains("weather") &&
(
statement.Contains("what") || statement.Contains("how")
)
)
{
statement = ("It's " + Weather.Get_Weather("condition") + " outside");
}
but it's not working... can any one help me with this, because I cannot find any mistake in this code and I don't even have that much experience with this type of operators like || and && because I can use If Statements inside If Statements.
And I want the statement to have Weather and What or How so I can confirm It's a Question, Or the user is asking for the weather...
You can Also gimme more ideas on this...
'how is the weather' this is not going through the If statement
it contains How and weather that's enough...
I am Really Sorry for my Question, It was My Problem I did not know that Contains() is Case-Sensitive...

Assuming the statement you're testing is actually 'How is the weather' then your if statement is working as expected. Your checks are seeing if the statement contains 'weather' and 'what' OR contains the word 'how' (note the lower case).
As your phrase doesn't contain the word 'what' the first check (for the words 'weather' AND 'what') will be false. Also, as the word 'How' starts with a capital 'H' it won't match against 'how', so will also return false and therefore not enter the if statement.
If you want your search to be case insensitive then you will need to consider the language as well, as words all in upper case in some languages mean different things to the same word in all lower case. Here's a similar question and answer, accepted answer detailed below for completeness:
To test if the string paragraph contains the string word (thanks
#QuarterMeister) culture.CompareInfo.IndexOf(paragraph, word,
CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo describing the language
that the text is written in.
This solution is transparent about the definition of
case-insensitivity, which is language dependent. For example, the
English language uses the characters I and i for the upper and lower
case versions of the ninth letter, whereas the Turkish language uses
these characters for the eleventh and twelfth letters of its 29
letter-long alphabet. The Turkish upper case version of 'i' is the
unfamiliar character 'İ'.
Thus the strings tin and TIN are the same word in English, but
different words in Turkish. As I understand, one means 'spirit' and
the other is an onomatopoeia word. (Turks, please correct me if I'm
wrong, or suggest a better example)
To summarise, you can only answer the question 'are these two strings
the same but in different cases' if you know what language the text is
in. If you don't know, you'll have to take a punt. Given English's
hegemony in software, you should probably resort to
CultureInfo.InvariantCulture, because it'll be wrong in familiar ways.

Related

How do I get two numbers between two words (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 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);

Porter stemmer algorithm in information-retrieval [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 need to create simple search engine for my application. Let's simplify it to the following: we have some texts (a lot) and i need to search and show relevant results.
I've based on this great article extend some things and it works pretty well for me.
But i have problem with stemming words to terms. For example words "annotation", "annotations" etc. will be stemmed to "annot", but imagine you try search something, and you will see unexpected results:
"anno" - nothing
"annota" - nothing
etc.
Only word "annot" will give relevant result. So, how should i improve my search to give expected results? Because "annot" contains "anno" and "annota" is slightly more than "annot". Using contains all the time obviously isn't the solution
If in first case i can use some Ternary search tree, in second case i don't know what to do.
Any ideas would be very helpful.
UPDATE
oleksii has pointed me to n-grams here, which may works for me, but i don't know how to properly index n-grams.
So the Question:
Which data structure would be the best for my needs
How properly index my n-grams
Stemming perhaps isn't much relevant here. Stemming will convert a plural to a singular form.
Given you have a tokeniser, a stemmer and a cleaner (to remove stop words, perhaps punctuation and numbers, short words etc) what you are looking at is a full-text search. I would advice you to get an off-the-shelf solution (like Elasticsearch, Lucene, Solr), but if you fancy a DIY approach I can suggest the following naive implementation.
Step 1
Create a search-orientated tokeniser. One example would be an n-gram tokeniser. It will take your word and split into the following sequences:
annotation
1 - [a, n, o, t, a, i]
2 - [an, nn, no, ot, ...]
3 - [ann, nno, not, ota, ...]
4 - [anno, nnot, nota, otat, ...]
....
Step 2
Sort n-grams for more efficient look-up
Step 3
Search n-grams for exact match using binary search

How to do password validation with regex? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Conditions :
Passwords must have at least 8 characters, which combine the use of at least 2 of the following: upper and lower case letters, numbers, and special characters.
Which pattern can fit with conditions?
Regular expressions are the completely wrong approach for this. Instead simply count the number of occurrences of each character type and then simply use if statements and boolean logic to check if your requirements are met.
However, reconsider if what you want to do is a good idea:
Restricting the symbols is a horribly bad idea. Any character should be allowed
When the password has a certain length, requiring e.g. symbols/numbers/mixed-case loses lots of its purpose. Additionally an attacker cannot know if some user uses just lowercase chars or just digits in his password and thus he cannot tune a brute-force attack to use only those chars - and since you'll hopefully be throttling incorrect logins brute-force is not a good option anyway.
Imagine "correct horse battery staple" from the famous xkcd. While all those words are in a dictionary and might even fail a improperly implemented password check, it's very secure. While a single dictionary word is extremely insecure multiple of them will be easy to remember and secure (an attacker would have to try all e.g. 4-word combinations which are A DAMN LOT even with just a 1000 word dictionary).
So a much better password policy would:
Reject obviously bad sequences. That's consecutive digits like 12345 or 54321. qwertz, qwerty, etc. are also bad.
Reject any password that can be found in a dictionary as a whole. Make sure to use both an english dictionary and one for each language your site supports.
Reject any other password that is likely to be insecure. Contains the username (even if backwards)? Nope. Contains the part before the # of the user's email address? Nope. Contains his birthdate? Nope.
Require at least 8 characters (as you already intend to do).
There's also an interesting post on the IT Security Stack Exchange site which you should read: Short complex password, or long dictionary passphrase?
string PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%]).{8,20})";
string password = "Password#1#";
Regex.IsMatch(password,PASSWORD_PATTERN);
try this.

Searching one criteria with many conditions C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ignoring accented letters in string comparison
I am using a special framework for making this job (NHibernate, Castle). My problem is that; there is a website where people can search apartments, rooms etc. Most of these website users are Turkish. So my problem starts here.
For example if people search the word: Beşiktaş(it is a district name) they can search like this Besiktas. As you can see there are some special characters(S,Ş Ö O, Ğ G, Ç C, İ I, Ü U ) in Turkish and people might be using both of them. I have to search all conditions like this. For example if they try to search Beşiktaş i have to search all variants like:"Besiktaş, Beşiktaş, Besiktas Beşiktas" and after this operation I have to remove duplicated objects from my list. How can I make this dream come true :)
I Just need the algorithm of this operation.
Sorry for my poor English skills. Thank You
You need to look into something called CultureInfo which can be leveraged to do this. Here is a similar question someone else asked regarding ignoring accented letters, which is pretty much the same as what you wish to do. Here is a method that was posted and accepted as an answer which will compare strings ignoring accented characters
string s1 = "hello";
string s2 = "héllo";
if (String.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0)
{
// both strings are equal
}
If you use this I would store the districts as their English names without accented letters and compare the string before you execute your query against your database. That way you would allow the user to use accented characters as well as their English names.
Here is a link to the MSDN article regarding CultureInfo

What is the regex for this? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
First of all I guess I will start by asking what are some good tools or references for building regex strings? I usually find them on the net, but I would love to learn them a little more.
Now on to my original question: what is the Regex to find a full string, or find a line that contains the string. The string is:
** Start of
The regex you are looking for is: \*\* Start of.*
Because C# has its own escape characters you may want to put this in a verbatim string like #"\*\* Start of.*".
The best tool for helping you build, learn and understand regular expressions is RegexBuddy. It helps you see the meaning of your expressions as well as test them through an intuitive graphical UI.
The most complete resource for information on regular expressions (across different languages) is http://www.regular-expressions.info/ . If you are looking to learn about a specific Regular Expression implementation you might be better of reading the implementation-specific documentation/spec. For .NET, a good starting place would be the Regex documentation at MSDN You can also test .NET regular expressions quickly online with the free tool available at http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
I also would like to note that I agree with #ziesemer that using an IndexOf or StartsWith method is probably a better solution for such a simple pattern.
I think you're using the wrong tool for the job. Regular expressions are best suited for finding patterns. It seems you're only looking to do a simple search - use the proper API (e.g. IndexOf) for this.
Otherwise, you simply need to escape the asterisks - which are special characters in regular expressions - meaning "match 0 or more of":
\*\* Start of
While very informative, none of the answers provide the correct regex for your specific problem. Here it is:
string regexPattern = #"^.*?\*{2} Start of.*?$";
Note that you will have to specify multiline option when searching for match.
You can see the results here.
And here's the explanation of the pattern:
^.*?\*{2} Start of.*?$
Options: ^ and $ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “*” literally «\*{2}»
Exactly 2 times «{2}»
Match the characters “ Start of” literally « Start of»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
For learning regex you could check the Regular Expression Basic Syntax Reference on www.regular-expressions.info and also additionally A Gentle Introduction: The Basics
And regarding the string to find if you want only character from a to z then I think you should write as
^[a-zA-Z]$
This will take small and capital a to z characters.
Update
^\*\* Start of(.*?)$
Spliting Detail
\*, take asterisk into consideration
Start of, compare exactly the this string
(.*?), take anything on that single line
^\*\* Start of(.*?)(([\n]*(.*?)){19})*$
Spliting Detail
\*, take asterisk into consideration
Start of, compare exactly the this string
(.*?)(([\n]*(.*?)){19})*, take anything but limit upto 19 lines

Categories