Page 1 of 1

I hope that someone can give me the solution

Posted: Sat Aug 31, 2013 7:57 am
by nkamp
Hello,

I'm a beginner with regex. I have a peace of code and there they use regex with preg_replace and preg_replace_callback.
- This is my string: 21 sep 15:30: team 1 - team 2
- At the moment (I have tried a lot) this is my regex: #((\d*) (.{3}))( (\d{2}):(\d{2}):)(.*)(\s\-\s)(.*)#
- And this is the result with preg_match:
array[0] - 21 sep 15:30: WSV DS 1 - Favorita DS 1
array[1] - 21 sep
array[2] - 21
array[3] - sep
array[4] - 15:30:
array[5] - 15
array[6] - 30
array[7] - team 1
array[8] - -
array[9] - team 2

What I want is this:
array[0] - 21 sep
array[1] - 15:30:
array[2] - team 1
array[3] - -
array[4] - team 2

Can somebody help me to get this in the right direction?

Thanks.
Nioc

Re: I hope that someone can give me the solution

Posted: Sat Aug 31, 2013 6:48 pm
by requinix
It's not possible to get that exact array but you can get

Code: Select all

array[0] - 21 sep 15:30: WSV DS 1 - Favorita DS 1
array[1] - 21 sep
array[2] - 15:30:
array[3] - WSV DS 1
array[4] - -
array[5] - Favorita DS 1
How? By removing the extra parentheses in your expression that aren't doing anything.

Code: Select all

#((\d*) (.{3}))( (\d{2}):(\d{2}):)(.*)(\s\-\s)(.*)#
  ^     ^        ^       ^
And with a small tweak for some unwanted spaces,

Code: Select all

#(\d* .{3}) (\d{2}:\d{2}:) (.*)(\s\-\s)(.*)#