I'm going nuts trying to get a regex to detect spam of keywords in the user inputs. Usually there is some normal text at the start and the keyword spam at the end, separated by commas or other chars.
What I need is a regex to count the number of keywords to flag the text for a human to check it.
The text is usually like this:
[random text, with commas, dots and all]
keyword1, keyword2, keyword3, keyword4, keyword5,
Keyword6, keyword7, keyword8...
I've tried several regex to count the matches:
-This only gets one out of two keywords
[,-](\w|\s)+[,-]
-This also matches the random text
(?:([^,-]*)(?:[^,-]|$))
Can anyone tell me a regex to do this? Or should I take a different approach?
Thanks!
Regular Expression allows you to match comma-separated-values, which gives you the flexibility to extract your data. This is a regular expression to simply match and valid comma-separated numbers in your data. I hope this Regular Expression will help people who have been finding a solution for matching comma-separated values.
But not a list with a trailing comma. My current regex: also matches lists with trailing commas ( aaa,bbb, ). I was thinking I could also do: but that is rather ugly. My real regex is not matching 3-letter-words, but something bigger, and repeating the regex is ugly.
If you want the position of the comma proceeding your input string use the following code: By feeding the match of the regular expression into the String Method indexOf ( you are able to locate the position of the start of your string. The usual approach is to use a pattern that cannot match your delimiter in place of ..
This regexp does not match, and hence do not consume, the delimiting commas. This regexp would match " and hence do not consume" in the previous sentence. The fact that your regexp matched and consumed the commas was the reason why your attempted regexp only matched every other candidate.
Pr your answer to my question, here is a regexp to match a string that occurs between two commas.
(?<=,)[^,]+(?=,)
This regexp does not match, and hence do not consume, the delimiting commas. This regexp would match " and hence do not consume" in the previous sentence.
The fact that your regexp matched and consumed the commas was the reason why your attempted regexp only matched every other candidate.
Also if the whole input is a single string you will want to prevent linebreaks. In that case you will want to use;
(?<=,)[^,\n]+(?=,)
http://www.phpliveregex.com/p/1DJ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With