Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a comma between two specific characters

Tags:

string

regex

php

I currently have the string:

"Blah, blah, blah,~Part One, Part Two~,blah blah"

I need to remove the comma between the ~ character so it reads.

"Blah, blah, blah,~Part One Part Two~,blah blah"

Can anyone help me out please?

Many thanks,

like image 959
James Doc Avatar asked Jun 25 '10 13:06

James Doc


People also ask

What is the shortcut to remove delimiter in Excel?

On the Home tab, click the Dialog Box Launcher next to Number. On the Number tab, in the Category list, click Number. To display or hide the thousands separator, select or clear the Use 1000 Separator (,) check box.

How do you trim a comma in SQL?

You can use right to check and see if the last character is a comma, and a case statment will either remove the last comma with a substring, or display the field if it does not end in comma.


2 Answers

If there is exactly one comma between the ~s and an even number of ~s in all, then

preg_replace("/~([^,]*),([^,]*)~/", "~\1\2~", $text) 

should do it.

like image 93
Jens Avatar answered Nov 14 '22 23:11

Jens


It may be easier to do this in a few steps:

  • Split on ~
  • Transform the parts that are "inside" the ~ only
    • Simply replace ',' with ''
  • Join the parts back together with ~

A regex solution

That said, it is possible to do this in regex, assuming an even number of ~:

<?php

echo preg_replace(
   '/(^[^~]*~)|([^~]*$)|([^,~]*),|([^,~]*~[^~]*~)/',
   '$1$2$3$4',
   'a,b,c,~d,e,f~,g,h,i,~j,k,l,~m,n,o~,q,r,~s,t,u'
);

?>

The above prints (as seen on codepad.org):

a,b,c,~def~,g,h,i,~jkl~m,n,o~qr~s,t,u

How it works

There are 4 cases:

  • We're at the beginning of the string, "outside"
    • Just match until we find the first ~, so next time we'll be "inside"
    • So, (^[^~]*~)
  • There are no more ~ till the end of the string
    • If there are even number of ~, we'll be "outside"
    • Just match until the end
    • So, ([^~]*$)
  • If it's none of the above, we're "inside"
    • Keep finding the next comma before ~ (so we're still "inside")
      • So, ([^,~]*),
    • If we find ~ instead of a comma, then go out, then go back in on the next ~
      • So, ([^,~]*~[^~]*~)

In all case, we make sure we capture enough to reconstruct the string.

References

  • regular-expressions.info/Character Classes, Anchors, Grouping and backreferences
like image 31
polygenelubricants Avatar answered Nov 14 '22 22:11

polygenelubricants