-2

For example I have the following string 1234567890

pattern is /1|2/

preg_match_all('/1|2/', '1234567890', $out)

gives

 [0] ⇒ "1"
 [1] ⇒ "2"

But I need to know only the number when the whole pattern occurs (1 time in this case). Is it possible to find it?

In other words I need to count how many times all pattern occurs


P.S.
for 'closers' and 'downvoters': the question is much wider then just regexpings

Another example: string sdjka1gsdf1la5wlkasdfcmjsdc8fgvkj I need to count how many times occurs set of letters a, b and c, followed by digits. As you can see in this example there'are three occurences - a1, a5 and c8. So how I can count them?

Example 2: String where 1 abcd when 1 123456 where 5 when 10 abdc I need to count how many times whe*+one digit occurs (in this case 3 times)

el Dude
  • 5,003
  • 5
  • 28
  • 40
  • 1
    Why is it one time? It is two times. – u_mulder Feb 28 '16 at 19:12
  • 1
    What do you mean by **whole pattern**? Your regex is `/1|2/` which means match either `1` or `2` – anubhava Feb 28 '16 at 19:13
  • @u_mulder I need to count how many times all pattern occurs – el Dude Feb 28 '16 at 19:34
  • @elDude still unclear. You might want to provide some clear input/expected output. Also maybe you're looking for `substr_count()` or `count_chars()` instead of a regex. – HamZa Feb 28 '16 at 20:06
  • @HamZa `substr_count` doesn't support `regexp`, and my patterns are complicated enough. I'll try to write some examples to make it more clear, thanx, though – el Dude Feb 28 '16 at 20:41
  • short example: I need to count `how many numbers followed by letters` – el Dude Feb 28 '16 at 20:42
  • @elDude then add a simple `count()` to your code https://eval.in/527096 ? – HamZa Feb 28 '16 at 21:06
  • that's convinient only while searching `letter+digit`. On other cases, I need to search for words or something more complicated – el Dude Feb 28 '16 at 21:33

1 Answers1

1

See preg_match_all manual:

Return Values
Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.

echo preg_match_all('/[abc]\d+/', 'sdjka1gsdf1la5wlkasdfcmjsdc8fgvkj');

3   demo at eval.in

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • How I can count words in this case? I mean, `multiple letters`? – el Dude Feb 28 '16 at 21:32
  • @elDude This is just up to the pattern you use. For your second example try [`whe\w*\s+\d\b`](https://regex101.com/r/yR4mD9/2). [See demo at eval.in](https://eval.in/527129). This pattern matches literal `whe` followed by any amount of `\w` word characters, followed by `\s+` one or more whitespaces, followed by `\d` one digit and `\b` a boundary. [Here is the regex faq](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). – bobble bubble Feb 28 '16 at 21:53
  • but when I looking for completely different words, then it seems impossible to put it in one pattern – el Dude Feb 28 '16 at 22:47
  • I just found that `/(word1|word2|word3)/`is counted like one occurence. – el Dude Feb 28 '16 at 23:32
  • @elDude The full pattern `1|2` matches two times in `1234567890`. Same like `[12]`. The full pattern `(word1|word2|word3)` matches 3 times in `word1 word2 word3 word4`. Same like `word[1-3]`. – bobble bubble Feb 29 '16 at 09:08