Very simple regex-curiosity question

I guess the forward slash doesn't mean what I thought it meant.  And I thought it meant nothing.

Let's say I have a URL like:


and I search this string for the regex /162/.

I thought that would match.  It doesn't.

regex 162 DOES match.

So what exactly do the forward slashes here mean, and why did my including them kill my ability to match?

What I am really wanting to do is the equivalent of *162*.  But I know that I probably don't need to tell regex [wildcard]162[wildcard], so in my feeble regex memory, I thought I had to use slashes to indicate that this is the pattern I'm trying to match anywhere in the text.

I'm basically fooling around and trying to do a string .contains, but using regex.  Meaning, just simply checking the source text to see if it contains a substring.

Thanks
bamapieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
then all you need is / and your string
i.e. /162/.

https://regex101.com/r/qZAJeL/2
Dr. KlahnPrincipal Software EngineerCommented:
The regex /162/. means:

Match the string "/162/" followed by any single character except end-of-line.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bamapieAuthor Commented:
My question has a typo...a period where I ended a sentence, but it looks like it's part of the regex.

The expression

/162/

is what doesn't match in the term

http://localhost:16246/

and I can't understand why.  Meanwhile, the same expression without slashes:

162

DOES work.  What's the difference?  Why do the slashes not match?  Don't they basically mean nothing?

Thanks
Dr. KlahnPrincipal Software EngineerCommented:
It does not match because:

The regex /162/. means:

Match the string "/162/".
 With the slashes.  Slashes are not string delimiters or special characters, so they are part of the match and must be matched for the regex to match.
käµfm³d 👽Commented:
It really depends on your regex engine. In Perl and Javascript, forward slashes are delimiters for the pattern. So given the following Javascript:

var reg = /abc/;

Open in new window


...the slashes mean nothing to the regex itself--they merely delineate the regex literal. In C# however:

Regex reg = new Regex("/abc/");

Open in new window


...now the slashes actually mean slashes in the target string (because slashes don't have special meaning in the C# regex engine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.