0

I have data in format:
Randomtext1 (random2,random4) Randomtext2 (ran dom) Randomtext3 Randomtext4 (random5,random7,random8)

So I would like to recognize the both the Randomtext-part and the text with in brackets, if it is there with preg_match_all, but im not sure how to get it done. The stuff in the brackets goes together with the Randomtext-part and some of the Randomtext-parts have it and some of them dont.

This is what I have so far, and it works in most cases, but if the text doesnt have any brackets, then it matches the whole thing to one.
preg_match_all("/([a-zA-Z]+)\s+(?([a-zA-Z, ]*))?/",$text,$matches);

Any ideas?

1 Answer 1

1
preg_match_all("/\b\w+\b(?:\s*\(.*?\)|)/", $text, $matches);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! With tiny changes I got the pattern changed to get just the information that I needed!
@Kemu79 - what is the change you have needed to do?
I changed the pattern to /\b(\w+)\b(?:\s*((.*?))|)/ to save the texts as I needed.

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.