pcre regex match commas not surrounded by quotes

hot_barbara asked
php regex pcre
via

Considering the following string:

Autauga,AL-Autauga,al,AL,"<Polygon><outerBoundaryIs><LinearRing><coordinates>-86.41182,32.4757 -86.41177,32.46599 -86.41167,32.45054 -86.41157,32.44245 -86.41154,32.43993 -86.41138,32.42573 -86.41135,32.42417 -86.41128,32.42185 -86.41117,32.41017 -86.41117,32.40994 -86.41615,32.4072 -86.43178,32.40132 -86.43926,32.40025 -86.44653,32.40036 -86.45876,32.40573 -86.4612,32.40285 -86.46247,32.38769 -86.46356,32.37729 -86.46836,32.37368 -86.47092,32.37136 -86.47306,32.36874 -86.47476,32.36588 -86.47777,32.36418 -86.48023,32.36497 -86.48342,32.36667 -86.4871,32.36674 -86.49047,32.36532 -86.49265,32.36286 -86.49263,32.36032 -86.49181,32.35787 -86.4908,32.35513 -86.48994,32.35264 -86.4899,32.34985 -86.49071,32.34705 -86.49308,32.34509 -86.49637,32.34451 -86.49677,32.34444 -86.49697,32.34441 -86.51978,32.34039 -86.54242,32.36286 -86.56658,32.37296 -86.58826,32.36775 -86.61646,32.38072 -86.61548,32.40307 -86.62671,32.40372 -86.64956,32.40055 -86.65426,32.37904 -86.66333,32.36937 -86.67726,32.35659 -86.68276,32.35356 -86.69448,32.35677 -86.71309,32.36186 -86.71624,32.36921 -86.71982,32.3884 -86.71721,32.40129 -86.73083,32.40061 -86.75543,32.38914 -86.78214,32.39047 -86.78238,32.37337 -86.7843,32.32449 -86.80352,32.30819 -86.82039,32.3184 -86.81563,32.3402 -86.81491,32.3408 -86.81229,32.34299 -86.81294,32.36582 -86.8318,32.38605 -86.8411,32.41849 -86.83652,32.4304 -86.85357,32.43893 -86.86242,32.43869 -86.86415,32.44592 -86.86997,32.4606 -86.87364,32.47188 -86.88497,32.48191 -86.88087,32.48886 -86.88465,32.49656 -86.89084,32.5124 -86.89979,32.52588 -86.90667,32.53685 -86.90521,32.54915 -86.90023,32.55933 -86.89905,32.57819 -86.90385,32.59242 -86.90759,32.60835 -86.91163,32.62085 -86.9119,32.63178 -86.9185,32.64452 -86.91827,32.65322 -86.91767,32.66411 -86.91759,32.66417 -86.91757,32.66417 -86.91241,32.66425 -86.8929,32.66343 -86.87358,32.66258 -86.84557,32.66137 -86.83382,32.66082 -86.81764,32.66016 -86.81319,32.66017 -86.78767,32.66052 -86.74998,32.66098 -86.72508,32.66159 -86.71349,32.66738 -86.71257,32.70574 -86.66704,32.70609 -86.63189,32.70633 -86.61778,32.70629 -86.60783,32.70632 -86.59515,32.70653 -86.57015,32.70678 -86.52944,32.70704 -86.4963,32.7073 -86.48468,32.7076 -86.47197,32.70778 -86.46803,32.70786 -86.45035,32.70809 -86.41583,32.70747 -86.41312,32.70739 -86.413,32.67818 -86.41298,32.67406 -86.41261,32.61636 -86.4125,32.58942 -86.41244,32.57816 -86.41234,32.55023 -86.41226,32.53344 -86.41223,32.52876 -86.4122,32.52668 -86.4123,32.52082 -86.41231,32.51273 -86.41234,32.50446 -86.4122,32.50006 -86.41212,32.49626 -86.41197,32.49065 -86.41197,32.48577 -86.41187,32.48112</coordinates></LinearRing></outerBoundaryIs></Polygon>",126.4044923,05000US01001,1001,"Autauga County, Alabama",1,1,1001.0,

What would be a pcre (php) regex that would select all the commas not surrounded by quotes? What I have so far is ,(?!d|-) but it’s not totally right. Here’s the regex101 link: https://regex101.com/r/ADLWfU/1 Thanks!


Answer
via

Here’s how to adjust your existing pattern.

(?<!"),(?!["d-])

And here’s how to do exactly as you asked, no surrounding quotes.

(?<!"),(?!")

Uses regex lookarounds.

Share This
Posted in: