0

I would like to remove a part of a string like in the example below (with a regex and preg_replace) :

abcd,{{any_word |same_word_but_to_keep}},efg...

Result should be :

abcd,{{same_word_but_to_keep}},efg...

Any idea?


Another example :

Bellevue,Bergouey |Bergouey(tokeep),Bourdious

Results should be :

Bellevue,Bergouey,Bourdious

Thanks a lot!!

6
  • what language? do you want this in Commented Jun 6, 2015 at 10:09
  • 1
    Until it is clear what is the pattern of the word or what are the words to be removed how are we supposed to answer. Commented Jun 6, 2015 at 10:09
  • thanks for your answer, I updated the question, maybe it's more clear like that, any idea how to build that regex? Commented Jun 6, 2015 at 10:13
  • Do you want any duplicates of a word following | to be removed? Could you add more examples? Commented Jun 6, 2015 at 10:13
  • yes, to remove the word duplicated with "|" just before the word to keep Commented Jun 6, 2015 at 10:15

4 Answers 4

2

You could use the following regex:

$str = 'Bellevue,Bergouey |Bergouey,Bourdious';
$str = preg_replace('~(\w+)\s*\|(\1)~', '$2', $str);
echo $str; //=> "Bellevue,Bergouey,Bourdious"
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

preg_match_all("/\|(.*?) /", $your_string, $matches);
foreach ($matches[1] as $match) {
    $your_string = preg_replace("/([^\|]|^)$match/", "", $your_string);
}
$your_string = preg_replace("/\|/", "", $your_string);

How it works

  • preg_match_all("/\|(.*?) /", $your_string, $matches) Gets all the words following |
  • preg_replace("/([^\|]|^)$match/", "", $your_string) Removes all occurrences of the match not preceded by | and accounts for if the matching word is at the start of the string with |^
  • preg_replace("/\|/", "", $your_string) Removes all occurances of | from the string

1 Comment

thank you OhAuth, but the words are not separated only with a space (can be also with a coma), I updated the question, the structure with duplicates is always between {{..}}, maybe it can make it eaisier, it's probably sthg stupid, any idea?
1

I'd do:

preg_replace('/(\w+),(\w+)\s*\|\2,(.+)/', "$1,$2,$3", $string);

Explanation:

  (\w+) : group 1, a word
  ,     : a comma
  (\w+) : group 2, a word
  \s*   : white spaces, optional
  \|    : a pipe character
  \2    : back reference to group 2
  ,     : a comma
  (.+)  : rest of the string

Comments

0

Finally I found a solution without a regex, it works perfectly :

$mystring="Arance,la Campagne,Gouze |Gouze,Lendresse";
$tmp="";
$words=explode(",",$mystring);
foreach($words as $word){
    if(strpos($word,"|")){
            $l=explode("|",$word);
            $tmp=$tmp.$l[1].",";
    }else{$tmp=$tmp.$word.",";}
}
$mystring=$tmp;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.