Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split text to match double quotes plus trailing text to dot?

How can I get a sentence that is in double quotes in which there is a dot that must be split?

Example document like this:

“Chess helps us overcome difficulties and sufferings,” said Unnikrishnan, taking my queen. “On a chess board you are fighting. as we are also fighting the hardships in our daily life.” he said.

I want to get output like this:

Array
(
    [0] =>"Chess helps us overcome difficulties and sufferings," said Unnikrishnan, taking my queen.
    [1] =>"On a chess board you are fighting. as we are also fighting the hardships in our daily life," he said.
 )

My code still explode by dots.

function sample($string)
{
    $data=array();
    $break=explode(".", $string);
    array_push($data, $break);

    print_r($data);
}

I'm still confused to split two delimiter about double quote and dot. because inside double quote there is a sentence that contain dot delimiter.

like image 721
Rachmad Avatar asked May 20 '17 06:05

Rachmad


People also ask

How do I get a double-quoted text value?

You can also get a double-quoted text value by using the split () method: const textWithQuote = 'One of my favorite quotes is "First we make our habits, then our habits make us". This quote is sometimes attributed to John Dryden, or other authors.' console.log( textWithQuote.split('"')[1])

What happens if there is a double quote in the middle?

Notice that if there is a double quote in the middle of a word it will be split (a"ctuall"y would be a, ctuall, y in the final result). If the last double quote is unmatch it won't split from its position to the end of the string.

How to extract text between double quotes from a string with JavaScript?

Learn how to extract text between double quotes from a string with JavaScript. Let’s say you want to get some quoted text from a text block. Quotes are usually wrapped by double quotes ( " " ). No problem, you can use JavaScript’s match () method and some RegEx magic.

How to add double quote on different words in Notepad++?

Add double quote on different words in notepad++. 1 use FIND: (?-s)^.+$ => REPLACE: "$0", my example will become*: 2 use FIND: (?-s)^.*$ => REPLACE: "$0", my example will become*:


1 Answers

A perfect example for (*SKIP)(*FAIL):

“[^“”]+”(*SKIP)(*FAIL)|\.\s*
# looks for strings in double quotes
# throws them away
# matches a dot literally, followed by whitespaces eventually


In PHP:
$regex = '~“[^“”]+”(*SKIP)(*FAIL)|\.\s*~';
$parts = preg_split($regex, $your_string_here);

This yields

Array
(
    [0] => “Chess helps us overcome difficulties and sufferings,” said Unnikrishnan, taking my queen
    [1] => “On a chess board you are fighting. as we are also fighting the hardships in our daily life.”
)

See a demo on regex101.com as well as a demo on ideone.com.

like image 84
Jan Avatar answered Oct 22 '22 06:10

Jan