2

I´m new to regular expressions, I need find one phrase in a piece of text (case insensitive), for example:

Text : FindThis("This is example text")

I need get "FindThis" to locate my phrase regardless of the case of the text.

I've tried this:

static Regex text= new Regex("(FindThis\\(['|\"])([^'\"]*)");
6
  • 2
    By default a Regex is done in a case sensitive manner. Can you try and explain what is going wrong by providing samples that you expect should match and don't? Commented Dec 17, 2013 at 17:03
  • Experiment. Sites like rubular.com are awesome for this. Regexes are, indeed, case sensitive by default (unless you add the i flag at the end). Commented Dec 17, 2013 at 17:05
  • Thanks for the responses, and I understand, the regular expression is case sensitive by default, but, in my example if I change the example test for "FiNdThIs("This is example text")" is not mached, I´m explain? Commented Dec 17, 2013 at 17:13
  • Are you saying that you'd like it to be matched regardless of case? Commented Dec 17, 2013 at 17:14
  • I say that in my example when I change one letter per upper or lower case, the regular expression stops working (in my example) Commented Dec 17, 2013 at 17:18

2 Answers 2

3

Case sensitive should be "on" by default. You can pass the option to ignore case when you do the match.

Here's an example: http://www.dotnetperls.com/regex

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response, but, in my example if I change the example test for "FiNdThIs("This is example text")" is not mached, I´m explain?
You'll need to post more code. You show the RegEx being created, but there should be a regEx.Match method with an input string. What's the rest of your code look like?
Don´t worry, I resolved this another way, anyway, thank you very much
1

You can do this by using the RegexOptions.IgnoreCase enum. Here's an example:

var result = Regex.IsMatch("Here's some FINDTHIS Text", // the text to search in
                "FindThis", // this is the text we're looking for
                RegexOptions.IgnoreCase); // specifies that it's not case sensitive

Note that in this case our regex pattern is actually just the text we're looking for. It could equally be a much more complex pattern.

I would check that the Contains method doesn't do what you're looking for? It's a lot simpler!

1 Comment

was not what I was looking for, but I used it anyway, thank you very much! (sorry for my english)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.