Regular Expression Matching contains special characters

Source: Internet
Author: User
Tags preg
{Code ...} traverse the $ info array and add ^, $ to the front and back of each string to a regular expression to match the provided $ message. However, because the string in $ info may have special characters, is there any way to escape every $ info string ?...
$ Info = array ("https://segmentfault.com/q/1010000003711515", "http://www.baidu.com? Admin. php & jasdhjas = asjd ", 'php $ a = 0; echo" abc "; '," suppose this is the third... * ^ % $ #@! () [] {} ",); Foreach ($ info as $ key => $ value) {$ message =" https://segmentfault.com/q/1010000003711515 "; $ preg ="/^ ". $ value. "$/"; if (preg_match ($ preg, $ message) {echo 'matched successfully'; break ;}}

Traversal$infoArray to add/^,$/To a regular expression to match the provided$messageBut because$infoThe character string in it may have special characters, causing errors. Is there any way$info?

Can only be defined$infoWill it be escaped?

Reply content:
$ Info = array ("https://segmentfault.com/q/1010000003711515", "http://www.baidu.com? Admin. php & jasdhjas = asjd ", 'php $ a = 0; echo" abc "; '," suppose this is the third... * ^ % $ #@! () [] {} ",); Foreach ($ info as $ key => $ value) {$ message =" https://segmentfault.com/q/1010000003711515 "; $ preg ="/^ ". $ value. "$/"; if (preg_match ($ preg, $ message) {echo 'matched successfully'; break ;}}

Traversal$infoArray to add/^,$/To a regular expression to match the provided$messageBut because$infoThe character string in it may have special characters, causing errors. Is there any way$info?

Can only be defined$infoWill it be escaped?

/The regular expression has a special meaning, so the '/' behind it will cause an error. you need to add a backslash to the front.\Cancel escape.

$ Preg is defined, but $ info is used. this is an error.
Also, the regular expression wildcard must be used.\Escape,
Your example above should be rewritten

$preg = "/^https:\/\/segmentfault\.com\/q\/1010000003711515$/";    $test = "https://segmentfault.com/q/1010000003711515";    preg_match($preg, $test);

I think it's better for you to say what you want to do, and you feel wrong.
$ Preg is an unknown variable.
Your premise is too big

If these expressions are input by programmers, they are a programmer's problem.

If these expressions are input by the user, you should inform the user on the input interface that the regular expression is used here, so that he can pay attention to them.

You can use js to escape the special characters of the regular expression, but it may cause two escape operations (for example, the user has already entered the converted content http: | b4831e42daf1db770cdbc01d%78e020 |/. +)
Example

document.getElementById('test').value = (new RegExp(document.getElementById('test').value)).toString();

In addition, adding/^ and $/in the program may also cause problems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.