0

For example

Season           // matches
Season/Winter    // wont match
Season/Spring    // wont match  
stema
  • 90,351
  • 20
  • 107
  • 135
RayLoveless
  • 19,880
  • 21
  • 76
  • 94
  • 4
    This regex is trivial. What have you tried? – Michael Dec 31 '12 at 18:09
  • 1
    @Michael And why don't you add an answer then? – Olaf Dietsche Dec 31 '12 at 18:11
  • 1
    @OlafDietsche Because it would be great practice for him/her to learn regex. Also, he/she didn't even show any work, so it seems like he didn't even attempt it and is just coming to us for a quick answer. Teach a man to fish, etc. – Michael Dec 31 '12 at 18:14
  • @Michael, Yeah you're probably right... I just use regular expressions so little that they drive me mad because I keep forgetting the systax.. Don't know if i'm the only one in this boat. – RayLoveless Dec 31 '12 at 18:34
  • @Raymo The best way to figure out how to use `regex` is to experiment yourself. Here's a site that let's you see what your regex is actually doing: http://www.regex101.com/. This is also another good one: http://www.regexplanet.com/. But first, I would recommend reading through this entire site: http://www.regular-expressions.info/ – Michael Dec 31 '12 at 18:37

2 Answers2

2

I really doubt you'd need a regular expression for that in any language, anyway:

^[^/]*$

http://regexr.com?339e2

You can usually use string search methods such as JavaScript's indexOf string method or PHP's strpos function to try to find /, most languages offer native string methods which are much faster and a better solution than regex, even more for something so simple.
Edit: For mongodb as OP's case, regex seems to be the most suitable solution though. I believe it should be more optimized than iterating through all records with another language's string function.

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • I'm need it for a php mongoDB query. I think a regex is the only solution here.. I'm open to suggestions though. – RayLoveless Dec 31 '12 at 18:17
  • Oh I see. I thought it could be a more specific use-case like that. `=]` I'll keep the note though so the other readers that simply look for a regex to match `/` in other languages will have that info. – Fabrício Matté Dec 31 '12 at 18:18
  • @Raymo Looking through this [related question](http://stackoverflow.com/q/3305561/1331430), seems like regex is actually the only way for this. – Fabrício Matté Dec 31 '12 at 18:23
  • It's also useful for a ruby gem's files, if you want to match only the `lib`, `bin`, and `test`/`spec`/`features` folders AND all files in the root. – Nate Symer Jul 31 '14 at 18:10
1

You're looking for a character class. These are the [ and ] characters. inside, you specify the characters you are looking for. You can also add the ^ character, to indicate a negated character class, where it contains anything BUT the one you've specified. So, your regex is:

^[^/]*$

However, most string libraries have tools to look for a particular character. For example, in C#, you can do:

if (! String.Contains("/")) { /* your logic here */ }
Robert P
  • 15,707
  • 10
  • 68
  • 112