I want to match punctuation in regular expressions, but I don't want to match punctuation in parentheses.

Asked 1 years ago, Updated 1 years ago, 43 views

I would like to split the string by ","
I want to match punctuation, but I don't want to match punctuation in parentheses

Example:
aa(bb,cc),dd,ff
↓(I want to match the '' part)
aa(bb,cc)', 'dd', 'ff

The environment is ruby 2.3

ruby regular-expression

2022-09-29 22:02

1 Answers

There might be another better way, but I came up with something like this.

"aa(bb,cc), dd,ff".scan(/[^,]*(.*)[^,]*|[^,]+/)
# = > ["aa(bb,cc)", "dd", "ff" ]


2022-09-29 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.