tags:

views:

55

answers:

1

Question

What does it mean when a regular expression is surrounded by @ symbols? Does that mean something different than being surround by slashes? What about when @x or @i are on the end? Now that I think about it, what do the surrounding slashes even mean?


Background

I saw this StackOverflow answer, posted by John Kugelman, in which he displays serious Regex skills.

Now, I'm used to seeing regexes surrounded by slashes as in

/^abc/

But he used a regex surrounded by @ symbols:

'@
        ^%
        (.{2})          # State, 2 chars
        ([^^]{0,12}.)   # City, 13 chars, delimited by ^
        ([^^]{0,34}.)   # Name, 35 chars, delimited by ^
        ([^^]{0,28}.)   # Address, 29 chars, delimited by ^
        \?$
 @x'

In fact, it seems to be in the format:

@^abc@x

In the process of trying to google what that means (it's a tough question to google!), I also saw the format:

@^abc@i

It's clear the x and the i are not matched characters.

So what does it all mean???

Thanks in advance for any and all responses,

-gMale

+4  A: 

The surrounding slashes are just the regex delimiters. You can use any character (afaik) to do that - the most commonly used is the /, other I've seen somewhat commonly used is #

So in other words, @whatever@i is essentially the same as /whatever/i (i is modifier for a case-insensitive match)

The reason you might want to use something else than the / is if your regex contains the character. You avoid having to escape it, similar to using '' for strings instead of "".

Jani Hartikainen
Good answer! If *i* is the modifier for case-insensitive search, what is the *x* a modifier for? Do you know of a place that would explain that aspect (the modifiers) in detail? All the regex stuff I google only covers the basics and I thoroughly know the basics.
gmale
The `x` modifier invokes free-spacing mode: http://www.regular-expressions.info/freespacing.html
Delan Azabani
@Delan: thanks, that's a fantastic source of advanced Regex information, generic across all languages! I'm adding this as a primary bookmark.
gmale