1

I've got these strings I want to parse:

?forum=Jiné akce a jiné#comments
?trening=140#$|Pralinka
?novinka=87#comments
?forum=Mimo mísu#comments
?forum=Členské forum#comments
?trening=139#comments

and I want to output array like

 1. forum 
 2. Jiné akce a jiné
 3. comments

or

 1. trening
 2. 140
 3. Pralinka

So I wrote following regexp:

\?([a-z]{4,})\=(.+)\#(\$\|)?([a-z]+)

Regex101

But It's not working in second case (optional string part).

3 Answers 3

3

Remember that by default, regex are case sensitive... So [a-z] can't match Pralinka. You can fix that by using the i (case insensitive) flag, or with:

\?([a-z]{4,})=(.+)#(?:\$\|)?([A-Za-z]+)

Notice that there is no need to escape the = or the # (we're not in free spacing mode), and I added a non capturing group (?:...) so that Pralinka will be in the same capturing group as comment.

The demo is here

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

1 Comment

Good, I didn't know about non-capturing groups. Thanks
2

[a-z]+ does not match Pralinka because P is an uppercase letter.

Fixed regex

Comments

0

You need to add a global flag: /g.

http://regex101.com/r/vR0oM4

1 Comment

See the second line, its not parsing it - thats the problem.

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.