1

Hello everyone I am trying to create a regexp for PHP that will allow me to get the quote and the author. I have made it work if everything is one line but the moment it is put in multiple lines it stops working. What am I doing wrong?

(\[quote\])(.*)(\|)(.*)(\[\/quote\])

[quote]Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.|-popular children's rhyme[/quote]

[quote]Silence you sleep.|-popular children's rhyme[/quote]

3
  • 1
    Use the s modifier (append if after the regex delimiter). Commented Nov 23, 2017 at 20:35
  • I am sorry @trincot is used ([quote])(.*)\S(\|)(.*)([\/quote]) and still didn't work. It still find the online but not the multiline Commented Nov 23, 2017 at 20:39
  • @Martin I am quite new with regex - where should I put the delimeters? I have tried the ([quote])(.*)\S(\|)(.*)([\/quote]) Commented Nov 23, 2017 at 20:44

2 Answers 2

5

Use the s modifier (append if after the regex delimiter). Also, make your .* non-greedy by adding a ?:

preg_match_all("~\[quote\](.*?)\|(.*?)\[/quote\]~s", $s, $results,  PREG_SET_ORDER);

Output is in $results:

[
    [
        "[quote]Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.|-popular children's rhyme[/quote]",
        "Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.",
        "-popular children's rhyme"
    ], [
        "[quote]Silence you sleep.|-popular children's rhyme[/quote]",
        "Silence you sleep.",
        "-popular children's rhyme"
    ]
]
Sign up to request clarification or add additional context in comments.

2 Comments

1. You don't need to escape | provided that it occurs outside of a group, nor the / if that's not also the delimiter. 2. You can make the attribution section optional with ~\[quote\](.*?)(?:\|(.*?))?\[/quote\]~s
@Sammitch, I take your points, except the first. An unescaped pipe (|) functions as an OR-operator whether it is inside or outside of a group -- in the latter case the OR concerns the whole pattern.
3

Code

See regex in use here

\[quote\](.*?)\|-(.*?)\[\/quote\]

Note: The regex above uses the s modifier. Alternatively, you can replace . with [\s\S] and disable the s modifier

Usage

$re = '/\[quote\](.*?)\|-(.*?)\[\/quote\]/s';
$str = '[quote]Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.|-popular children\'s rhyme[/quote]

[quote]Silence you sleep.|-popular children\'s rhyme[/quote]';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

Results

Input

[quote]Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.|-popular children's rhyme[/quote]

[quote]Silence you sleep.|-popular children's rhyme[/quote]

Output

The output below is organized by group (separated by newline)

Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.
popular children's rhyme

Silence you sleep.
popular children's rhyme

Explanation

  • \[quote\] Match this literally (backslashes escaping the following character)
  • (.*?) Capture any character any number of times, but as few as possible into capture group 1
  • \|- Match this literally (backslashes escaping the following character)
  • (.*?) Capture any character any number of times, but as few as possible into capture group 2
  • \[\/quote\] Match this literally (backslashes escaping the following character)

1 Comment

$parsed_string = "[quote] Silence in the boatyard, |-popular children's rhyme[/quote]"; $pattern = '/[quote](.*?)\|-(.*?)[\/quote]/s'; $parsed_string = preg_replace($pattern,'<blockquote>\2<div class="author">-- \4</div></blockquote>', $parsed_string); I am trying to use this but I am only getting the second part "popular children's rhyme "

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.