Add text to string using regular expression(preg_replace:php) with restricted words

两盒软妹~` 提交于 2019-12-11 07:45:45

问题


I have a boolean search string for third party index search service: Germany or (Indian, Tech*)

I want my result to be after processing: Germany[45] or (Indian[45], Tech*[45]). Here 45 is the weight needed by the search service.

After googling around for long I was able to get the result: Germany[45] or (Indian[45], Tech[45]*). Here you can see * has came after [45] which is not required.

The output should be: Germany[45] or (Indian[45], Tech*[45]), look for * before [45].

Code:

preg_replace('/([a-z0-9\*\.])+(\b(?<!or|and|not))/i', '$0'."[45]", $term);

So the simple concept behind it is to apply weight to words, but not to or/and/not etc. boolean search sensitive words. Please help me to fine tune the regexp or give a new regex to get required result.


回答1:


The problem was that you were only getting matches that include a \b - a word boundary. Since an asterisk is a non-word character, it was eliminating it from the match, so the solution was to allow for either a word boundary or an asterisk (\*|\b):

preg_replace('/([a-z0-9.]+)((\*|\b)(?<!or|and|not))/i', '$0'."[45]", $term);

However, it's simpler to do it with a negative lookahead:

preg_replace('/\b(?!or|and|not)([a-z0-9*.]+)/i', '$0'."[45]", $term);

Note: Within character classes asterisks and periods are not metacharacters, so they don't need to be escaped as you had in your original expression: [a-z0-9\*\.]+.




回答2:


Using a lookahead works like a charm:

preg_replace('/\b(?!or|and|not)([a-z0-9*.])+/i', '$0'."[45]", $term);

You can try it HERE

Edit: Also no need to escape "*" and "." inside a character class

Note that the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.

Source: http://www.regular-expressions.info/



来源:https://stackoverflow.com/questions/18231256/add-text-to-string-using-regular-expressionpreg-replacephp-with-restricted-wo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!