tags:

views:

23

answers:

1

How do i write a regex that matches the syntax in either perl ruby javascript

The syntax is like /my pattern here/modifiers where modifiers are i, g and other single letters.

I wrote the below however that wouldn't allow me to escape escape / with using . AFAIK i can write /Test\/Pattern/i in those languages to match the text Test/Pattern

/[^/]+/[^ ]*
+2  A: 

You need to take into account that the / might occur in the regular expression. But there is must be escaped. So:

/([^\\/]|\\.)*/[^ ]*

Here ([^\\/]|\\.) matches either any character except \ and / or an escaped character.

Furthermore, you often don’t need to escape the / if it’s inside a character class, so:

/([^\\/[]|\\.|\[([^\\\]]|\\.)*])*/[^ ]*

Here \[([^\\\]]|\\.)*] matches a literal [ followed by zero or more either any character except \ and ] or an escape sequence, followed by the literal ].

Additionally, modifiers are only alphabetic characters:

/([^\\/[]|\\.|\[([^\\\]]|\\.)*])*/[a-zA-Z]*
Gumbo
i'm so confused. I see () around it, but [] around everything inside which strikes me as unusual. Then there is a [] in there... and why is it | .? whouldnt that ruin catching the last /? if i had two regex in the same line i wouldnt want it to start at the first and end at the second but anyways i only wanted to prevent ending it if / is not escaped with \. and have \ before the end without escaping / (i guess this syntax: \\/). But you have me an idea how to do this. +1. Will post in a second.
acidzombie24
I plugged it into http://gskinner.com/RegExr/ and even though i dont understand it, i see it works. Now i'll break it down and see what is going on. +1 and accept
acidzombie24
Now i completely understand it. Thanks :)
acidzombie24
@acidzombie24: If you have special cases like here the escape sequence and character classes that can contain a `/`, you need to exclude them from the general characters. Here `[^\\/[]` is the character class of anything but `\‍`, `/`, and `[`. So the characters `\‍` and `[` are not mistakenly taken as “normal” characters as they mark the initial characters of the special cases. And `\\.` is just an escape sequence (literal `\‍` followed by any character).
Gumbo
@acidzombie24: If you need some further explanation, please fell free to ask.
Gumbo