14 Replies - 902 Views - Last Post: 25 August 2010 - 07:04 PM Rate Topic: -----

#1 Guest_ice*


Reputation:

help with regular expression

Posted 23 August 2010 - 11:46 PM

I have a string like this-
hell {aak|kka} | {kk|kak} kakkd

and i am trying to replace | inside the {} with comma.

i have tried for hours, but not succeed. plz some one help me.
Is This A Good Question/Topic? 0

Replies To: help with regular expression

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: help with regular expression

Posted 23 August 2010 - 11:56 PM

why not using str_replace()?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_ice*


Reputation:

Re: help with regular expression

Posted 24 August 2010 - 12:09 AM

I have tried with str_replace, but how will i determine the | inside the {}?
Was This Post Helpful? 0

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: help with regular expression

Posted 24 August 2010 - 12:29 AM

ah, ok, now I see the problem

you would have to escape those characters ({, } and | have a special meaning in RegEx)
Was This Post Helpful? 0
  • +
  • -

#5 Guest_ice*


Reputation:

Re: help with regular expression

Posted 24 August 2010 - 12:33 AM

i was trying for few hours with various patterns and yet to figure the correct one. i have to replace the | inside the {} and those | outside {} need to unchanged. Any help is appreciated.
Was This Post Helpful? 0

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: help with regular expression

Posted 24 August 2010 - 12:35 AM

what RegExes have you already tried?
Was This Post Helpful? 0
  • +
  • -

#7 Guest_ice*


Reputation:

Re: help with regular expression

Posted 24 August 2010 - 12:38 AM

A variation of this-

$str="hell {aak|kka} | {kk|kak} kakkd";

$pattern="{(.*)\|(.*)}";

$str=ereg_replace($pattern,",",$str);


Was This Post Helpful? 0

#8 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: help with regular expression

Posted 24 August 2010 - 12:39 AM

preg_replace('/\{.*(\|).*\}/g', ',', $your_string);
Was This Post Helpful? 0
  • +
  • -

#9 Guest_ice*


Reputation:

Re: help with regular expression

Posted 24 August 2010 - 12:42 AM

$str="hell {aak|kka} | {kk|kak} kakkd";
$str=preg_replace('/\{.*(\|).*\}/g', ',', $str);



this gives unknown modifier g, i replaces g with i and result is - hell , kakkd
Was This Post Helpful? 0

#10 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: help with regular expression

Posted 24 August 2010 - 01:43 AM

This should do it.

You need to grab everything from { to | and from | to }, backreference them as $1 and $2 and use that as your replacement string.
Here I'm checking for anything that's not | or } rather than just using anything .* because that's greedy and would potentially match the entire string.

$str = preg_replace('/(\{[^}|]+)\|([^}]+\})/', '$1,$2', $str);


Was This Post Helpful? 1
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: help with regular expression

Posted 24 August 2010 - 02:56 AM

note: a RegEx string requires delimiters (moopet used "/", but you can use anyone you like) to indicate the beginning and end as well as the RegEx modifyers (g, i & m).
Was This Post Helpful? 0
  • +
  • -

#12 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: help with regular expression

Posted 24 August 2010 - 12:11 PM

str_replace still works but its a two-step process. First, the string to replace is "|" and the replacing string is ",". Next, the string to replace is "} , {" and the replacing string is "} | {". Save yourself the hassle of regular expressions if they're not actually needed. It also helps other programmers understand your code better rather than having to decipher your particular regex pattern.

This post has been edited by grimpirate: 24 August 2010 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#13 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: help with regular expression

Posted 24 August 2010 - 12:47 PM

View Postgrimpirate, on 24 August 2010 - 06:11 PM, said:

str_replace still works but its a two-step process. First, the string to replace is "|" and the replacing string is ",". Next, the string to replace is "} , {" and the replacing string is "} | {". Save yourself the hassle of regular expressions if they're not actually needed. It also helps other programmers understand your code better rather than having to decipher your particular regex pattern.


Half the fun of regular expressions is that they make you look suave :)

Seriously, I agree that if you can get away with str_replace() you probably should, but in this instance I think a regex (with a comment explaining its purpose, maybe) would be better. In his example string it doesn't demonstrate that there will always be a fixed space-pipe-space between braces. If there is a newline character or an extra space, for example, str_replace will not cope.
Was This Post Helpful? 0
  • +
  • -

#14 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: help with regular expression

Posted 24 August 2010 - 08:16 PM

You're making unwise assumptions about the algorithmic parameters moopet. Your assumptions provide for greater flexibility, but I only allow as much flexibility as the problem stipulates. Its my opinion that using complex functions to achieve the same result is a poor programming practice regardless of circumstance.

The OP may not have included in his original message that the string will vary as you are saying. However, I don't want people who visit this thread to think that simpler funtions (which presumably use less clock cycles) in code is a methodology of "getting away" with something.

Using less RAM is far more suave than cool functions.
Was This Post Helpful? 0
  • +
  • -

#15 Dannyboy997  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 64
  • View blog
  • Posts: 258
  • Joined: 17-April 09

Re: help with regular expression

Posted 25 August 2010 - 07:04 PM

I agree with both moopet and grimpirate.

As a programmer you would want to achieve what you can with the less effort and that takes the less time.
But sometimes less time = worse code which breaks a program (speaking for online and offline).

So what I'm trying to say is that,(IN MY OPINION) to use the way that takes the less time to a certain point.

But this is really up to a matter of opinion.

P.S I know this was off topic!

This post has been edited by Dannyboy997: 25 August 2010 - 07:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1