Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match text between commas

Tags:

regex

php

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!

like image 617
SkarXa Avatar asked Oct 22 '13 08:10

SkarXa


People also ask

How to match comma-separated values with regular expression?

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.

Does your regex match lists with trailing commas?

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.

How to find the position of the comma in a string?

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

Why does this regexp not consume the delimiting 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.


1 Answers

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

like image 143
Taemyr Avatar answered Nov 06 '22 20:11

Taemyr