I hope that someone can give me the solution

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
nkamp
Forum Newbie
Posts: 3
Joined: Sat Aug 31, 2013 7:48 am

I hope that someone can give me the solution

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I hope that someone can give me the solution

Post 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)(.*)#
Post Reply