Regexp matches all specified characters only between quotation marks

Login: "string&number one&two" & "word&number two&three".

The desired conclusion "string number one two" & "word number two three".

In other words, the regular expression should match &only in the quoted phrases and remain between them. I was able to do only the following:

(?<=^|\s").*?([&])[^"]+

But there is an unwanted expression that matches only the first &in each phrase, if I use a greedy regular expression, it will only match the last &in the entire line.

How can I do it? I really tried many ways and lost my patience.

+4
source share
5 answers

& " .

"string&number one&two" & "word&number two&three"
       3 " ahead -> uneven -> inside
                  3 " ahead -> uneven -> inside
                        2 " ahead -> even -> outside
                               1 " ahead -> uneven -> inside
                                          1 " ahead -> uneven -> inside

A lookahead &, " , $ .

&(?!(?:[^"]*"[^"]*")*[^"]*$)
  • (?!
  • (?:
  • [^"]* , "

. regex101

0

. .

var str = '"string&number one&two" & "word&number two&three" & "string regex&test"';
str.replace(/&(?=[^"]+?\S")/g,' ');
console.log(str); 
// '"string number one two" & "word number two three" & "string regex test"'
+2

:

var str = '"string&number one&two" & "word&number two&three"';
var result = str.replace(/(\S)&(\S)/g, "$1 $2");
console.log(result);

;

"string number one two" & "word number two three"

javascript, .

0

For the pleasure of this, here is the Oracle burette solution:

with tbl(str) as (
  select '"string&number one&two" & "word&number two&three"' from dual
)
select regexp_replace(str, '(\S)&(\S)', '\1 \2') fixed_string
from tbl;

Of course, if ampersands inside quotes may have spaces around them, this solution fails, but this is not a requirement.

0
source

You cannot do this with regex only. You will need to write code that looks for a string between quotation marks, replace with a &space and move on.

-1
source

All Articles