0

I want to match the Property Name and Property value in the string below

#Property Name : Property Value

The Property Name and Property Value are single/multi-worded sentence with numbers and spaces only. No special characters in them.

I tried with (?<=#)(.*):(.*?) but it is not working. I read through many of the questions in this website and tried them, but none seemed to work.

I expect the answer to use QRegExp (RegEx class of Qt) of qt4

2 Answers 2

1

IMO this is better:

^#\s*([^:]+?)\s*:\s*(.*?)\s*$

https://regex101.com/r/pW3wV4/3

See this to why my solution is better (same reg exp but more test cases).

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. This works too! Can you explain why your expression is better? As I see it takes care of the leading and trailing spaces and also other special characters. Is that it?
here are examples proving why my regexp is better. Give more data so it could be even more polished.
Awesome example. Thanks again! It covers all my requirements
this is not working now. QRegExp.isvalid() returns false. So the regex is not running in qt. But it is working in online regex editors. Any help to solve the issue?
I'm pretty sure you've messed up the back slashes. Create new question with complete code, and add link here. Note there is new API in Qt5: QRegularExpression.
|
1

Try the following regex

^#([a-zA-Z0-9 ]*) : ([a-zA-Z0-9 ]*)$

demo here: https://regex101.com/r/gS7rF5/2

2 Comments

@Amen, Thanks, it works! But I don't know why (?<=#)(.*):(.*) which worked in the online regex tester didn't work for QRegExp. Any ideas?
I will wait for some other answers to the question for a while.

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.