Pcre regex to match the first two words, numbers

I need a regular expression to match only the first two words (they can contain letters, numbers, commas and other punctuation marks, but not white spaces, tabs or new lines) per line. My solution is ([^\s]+\s+){2} , but if it matches something like: "123 word '* in" 123 word, hello' *, it does not work with a two-word string and without spaces after.

What is the correct regular expression for this task?

+4
source share
3 answers

This is almost correct for you:

 ([^\s]+\s+[^\s]+) 

Assuming you don't need more control over which characters to use.

If you need to combine both words or just one word, you can use one of them:

 ([^\s]+\s+[^\s]+|[^\s]+) ([^\s]+(?:\s+[^\s]+)?) 
+3
source

Instead of trying to match words, you can split the string into spaces with preg_split() .

+2
source

If you really want to allow numbers and the letters [^\s] not restrictive enough. Use this:

 /[a-z0-9]+(\s+[a-z0-9]+)?/i 
0
source

Source: https://habr.com/ru/post/1439479/


All Articles