0

I have a sentence with varying lengths of words. The x in the sentence below represents random words, and the x1,x2, and x3 represent fixed words - words that don't change. Basically, x1 is x1, x2 is x2, and x3 is x3. First, I need to check whether there is a x in between x1, x2 and x3. If there is a x in between them, I need the x's value. That's it. How would I do that?

x x1 x x2 x3 x

P.S There can be more than 1 x in between x1, x2 and x3, and also can be more than 1 x to the left and right.

Jonny 5
  • 12,171
  • 2
  • 25
  • 42
jessica
  • 1,667
  • 1
  • 17
  • 35
  • 1
    How about a few real examples? `x` might be numeric, alphabetic, alpha-numeric, UTF-8, etc. And what have you tried? – bishop Aug 25 '15 at 15:13
  • Do you want to retrieve the x's outside as well? That would contradict the title of your question. – syck Aug 25 '15 at 15:29
  • 2
    @jessica Added [regex] tag to your question. Please provide more details: Input samples; Could there be different words in between? Would you need each different word that could occure, or is only allowed a sequence of repeating words in between the "fixed" words. How to deal with preceding words before `x1` and words after `x3`? What is your exact task? – Jonny 5 Aug 25 '15 at 15:30

1 Answers1

1
preg_match_all('/x1\\s+(.+?)\\s+x2\\s+(.+?)\\s+x3/i', $string, $matches);

will put your desired content into $matches[1] (matches between x1 and x2) and $matches[2] (matches between x2 and x3). The regular expression searches for all occurences of x1 followed by whitespace, something else, whitespace, then x2, another whitespace-anything-whitespace-sequence and x3 at last.

If you want the strings in-between as seperate words, you could do a preg_split('/\s/', ...) on them. The regular expression from above can also be adapated to that, but that would make retrieval more complicated.

Example:

<?php

$string = 'The quick brown fox jumps over the lazy dog';
preg_match_all('/quick\\s+(.+?)\\s+fox\\s+(.+?)\\s+lazy/', $string, $matches);
var_dump($matches);

?>

yields

array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(35) "quick brown fox jumps over the lazy"
  }
  [1]=>
  array(1) {
    [0]=>
    string(5) "brown"
  }
  [2]=>
  array(1) {
    [0]=>
    string(14) "jumps over the"
  }
}

which is imho the correct result.

As you can see, $matches[1][0] contains the words between quick (or x1) and fox (or x2), and $matches[2][0] contains the words between fox (or x2) and lazy (or x3). If there are more occurences found, they will be stored under $matches[1][1] and $matches[2][1] and so on, counting up the second index. It will be sufficient to iterate over the indexes of $matches[0], as all result sets will contain the complete match and the two partial matches.

syck
  • 2,984
  • 1
  • 13
  • 23
  • What's $matches? An array? And it'll put it in $matches[2]? $matches with an index of 2? Why an index of 2, and not 0? – jessica Aug 25 '15 at 15:12
  • Please explain all the squiggly symbols in between this: /(x1|x2|x3)\s+(.*?)\s+(x1|x2|x3)/ – jessica Aug 25 '15 at 15:13
  • Have a look at http://php.net/manual/en/function.preg-match-all.php and there at PREG_PATTERN_ORDER: All matches that are found by the secound parenthesized subpattern will be contained by $matches[2]. – syck Aug 25 '15 at 15:15
  • You know that x has to be in between x1 x2 and x3, right? It can't be anywhere else. – jessica Aug 25 '15 at 15:15
  • So it is not sufficient to have `x1 x x2` or `x1 x x3` or `x2 x x3`? Or `x2 x x1` etc.? – syck Aug 25 '15 at 15:17
  • It is not sufficient. It has to be x1 x x2 x3. And there can be more than 1 x in between x1, x2 and x3. – jessica Aug 25 '15 at 15:19
  • Okay, then I adapt my answer. But x can be anything, right? – syck Aug 25 '15 at 15:21
  • Let me know if you figured it out or given up. – jessica Aug 25 '15 at 15:29
  • The expression in my (now adapted) answer should fulfill your needs. – syck Aug 25 '15 at 15:31
  • It's not. It's just returning an empty array. $string = "x1 x x2 x3"; preg_match_all('/x1\s+(.+?)\s+x2\s+(.+?)\s+x3/', $string, $matches); print_r($matches); – jessica Aug 25 '15 at 15:36
  • Sorry, I forgot that in PHP double backslashes must be used. Corrected. – syck Aug 25 '15 at 15:40
  • It's still showing an empty array: $string = "x1 x x2 x3"; preg_match_all('/x1\\s+(.+?)\\s+x2\\s+(.+?)\\s+x3/', $string, $matches); print_r($matches); – jessica Aug 25 '15 at 15:41
  • Can you just use the x1,x2, and x3, in the example? I also need it to be case-insensitive. – jessica Aug 25 '15 at 15:47
  • I just need the x in between x1 x2 and x3. This array is returning 3 results, and none of them is between x1 x2 and x3. – jessica Aug 25 '15 at 15:49
  • I put an `i` modifier at the end of the regular expression, to make it case insensitive. Thanks for the appreciation. – syck Aug 25 '15 at 15:58