I would like to know why preg_match('/(?<=\s)[^,]+(?=\s)/',$data,$matches);
matches "List Processes 8989" in the string "20180513 List Processes 8989". The regex I am using should not match numeric characters. What is wrong?
Asked
Active
Viewed 104 times
-3

Artūras Kalandarišvili
- 113
- 1
- 10
-
So what should it match? – revo May 13 '18 at 21:05
-
@revo It should match "List Processes" only. – Artūras Kalandarišvili May 13 '18 at 21:08
-
What makes you think it shouldn't match numeric characters? What part of your regex excludes numeric characters? The `[^,]` basically means: _Any_ characters _except_ `,`. – 41686d6564 stands w. Palestine May 13 '18 at 21:10
-
Please be clear about the input and desired output. – Pedro Lobito May 13 '18 at 21:11
-
If you expect "List Processes" only then it means there is an space at the end where digits end. – revo May 13 '18 at 21:13
-
@ArtūrasKalandarišvili For your given case: "`20180513 List Processes 8989`", the regular expression above [actually doesn't match the digits](https://regex101.com/r/jZ3hqJ/1/), but that's because there's no space after the digit in the given string. – Graham May 13 '18 at 21:17
-
I encourage you to `var_dump($data)` and see what's going on. – revo May 13 '18 at 21:17
1 Answers
2
The [^,]
basically means any character except ,
. If you want to exclude numeric characters as well, you can replace it with [^,0-9]
, or better [^,\d]
, so your regex would look like this:
(?<=\s)[^,\d]+(?=\s)
I'm assuming the input string in your question is only part of the actual input string you're using because the regex you provided won't match the numbers at the end unless they're followed by a whitespace.
References:

41686d6564 stands w. Palestine
- 19,168
- 12
- 41
- 79