Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ronronmx · May 30, 2011 at 09:59 PM · booleanif-statementsregex

How to use the Regex class to check for string with case insensitive?

I'm a little stuck on this one, and I'm sure it's actually very easy to do...but I am missing something.

Basically I need to check for a string, but without case sensitive:

 string input = "Track 01";
 
 if( input == "track 01")
 {
   //do something
 }

right now the above if statement returns false, because I should be looking for a capital "T". What i want to do is make the above if statement return true whether the input is "Track 01" or "track 01", or even better, "track01".

I looked at the Regex class on the MSN library but I'm a little confused as to how I would use it in my scenario? I guess I don't really understand the tutorials on the use of the Regex class.

I have also read that "(?i)" before the string is the same as using RegexOptions.IgnoreCase flag, but again, I couldn't make it work.

Any help would be greatly appreciated! Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by yoyo · May 31, 2011 at 05:05 AM

Put this at the top of your file ...

 using System.Text.RegularExpressions;

And then do your comparison like this ...

 string input = "Track 01";
 
 if( Regex.IsMatch(input, "track 01", RegexOptions.IgnoreCase) )
 {
   //do something
 }

Note that any string containing the "track 01" pattern will also match. If you want to match the entire string you need to anchor the pattern to the start and end of the line, with "^track 01$". Regular expressions are a complex language of their own -- powerful, but not always intuitive.

If all you want is case-insensitive comparison then this is much simpler:

 if( String.Compare(input, "track 01", true) )
 {
   // do something
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · May 31, 2011 at 09:53 PM 0
Share

Awesome, thx a lot yoyo! I just started scratching the surface of Regular expressions and yes, they are a complex language which confuses the heck out of me haha.

Your answer was exactly what I needed, very clear and easy to follow, thanks again!

avatar image mehowe7 · Jan 20, 2012 at 04:57 PM 0
Share

how would i check that the user has pressed the enter/return key? I've tried using

if(Regex.Is$$anonymous$$atch(stringToEdit, "done")){

but it doesn't work. using a letter such as z does what i want so the rest of the code is fine, i just cannot figure out the shortcut for the enter key. I've also tried using fire 2 and setting that up in the input manager to be enter but that doesn't work either

avatar image Mystomex · Nov 15, 2016 at 06:26 PM 0
Share

I know I am quite late to the party on this one but for anyone else that stumbles across this question/answer and wants a little more information about Regex expressions and usage check out this interactive tutorial series which $$anonymous$$ches a good portion of the hows with this amazing little engine. https://regexone.com/lesson/introduction_abcs

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

check guitexture 2 Answers

Check playerAlive var on if statement. "Error: Operator &&' cannot be applied to operands of typebool' and `int'" 1 Answer

How to Set a bool to Individual Scenes (C#) 1 Answer

if statements is not working properly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges