Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
720 views
in Technique[技术] by (71.8m points)

php - What does it mean when a regular expression is surrounded by @ symbols?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...