views:

138

answers:

6

Regex fun again...

Take for example http://something.com/en/page

I want to test for an exact match on /en/ including the forward slashes, otherwise it could match 'en' from other parts of the string.

I'm sure this is easy, for someone other than me!

EDIT:

I'm using it for a string.match() in javascript

+1  A: 

Any reason /en/ would not work?

Chris Ballance
It would not work since the slash is integral part of the regex hence the need to escape it as per Parappa's answer as the slash is used as an indicator in regex generally, on most posix compliant systems such as unix, there would be extra info supplied, for example to search for a string ignoring upper/lower case would be /some_string/i.
tommieb75
And also it is based on what programming language, see TheLameDuck's answer. Definitely for C#, you would need to escape the forward slash.
tommieb75
Though I appreciate the kudos, it is only the backslash \ that would need to be escaped.Plus, you could always sidestep the whole issue if there were an issue by making it a string literal, ie string test = #"my back \ slash";
Michael La Voie
What? No. No need to escape the *forward* slash in a C# string.
Tim Pietzcker
I just saw a typo in my comment. It should have been: string test = @"my back \ slash";
Michael La Voie
This is not a Regex, just a standard string match.
Chris Ballance
+2  A: 

You may need to escape the forward-slashes...

/\/en\//
Parappa
Just do a string match, Regex is overkill for this case.
Chris Ballance
+2  A: 

Well it really depends on what programming language will be executing the regex, but the actual regex is simply

/en/

For .Net the following code works properly:

string url = "http://something.com/en/page";

bool MatchFound = Regex.Match(url, "/en/").Success;

Here is the JavaScript version:

var url = 'http://something.com/en/page';
if (url.match(/\/en\//)) {
    alert('match found');
}
else {
    alert('no match');
}

DUH

Thank you to Welbog and Chris Ballance to making what should have been the most obvious point. This does not require Regular Expressions to solve. It simply is a contains statement. Regex should only be used where it is needed and that should have been my first consideration and not the last.

Michael La Voie
Perfect, thanks.. I was having problems with escaping my slashes.
Ben
Using `regex.test()` is less costly than `string.match()`
J-P
Using a simple String.Contains("/en/") is less costly than using a Regex at all...
Chris Ballance
A: 

/\/en\// or perhaps /http\w*:\/\/[^\/]*\/en\//

DigitalRoss
+3  A: 

If you're trying to match /en/ specifically, you don't need a regular expression at all. Just use your language's equivalent of contains to test for that substring.

If you're trying to match any two-letter part of the URL between two slashes, you need an expression like this:

/../

If you want to capture the two-letter code, enclose the periods in parentheses:

/(..)/

Depending on your language, you may need to escape the slashes:

\/..\/
\/(..)\/

And if you want to make sure you match letters instead of any character (including numbers and symbols), you might want to use an expression like this instead:

/[a-z]{2}/

Which will be recognized by most regex variations.

Again, you can escape the slashes and add a capturing group this way:

\/([a-z]{2})\/

And if you don't need to escape them:

 /([a-z]{2})/

This expression will match any string in the form /xy/ where x and y are letters. So it will match /en/, /fr/, /de/, etc.

In JavaScript, you'll need the escaped version: \/([a-z]{2})\/.

Welbog
+1 for pointing out that Regexps are not necessary for exact matches.
Heinzi
A: 

You don't need a regex for this:

location.pathname.substr(0, 4) === "/en/"

Of course, if you insist on using a regex, use this:

/^\/en\//.test(location.pathname)
Eli Grey