0

I have a multi-line string shown below -

   ?a="text1
    ?bc="text23

I need to identify a pattern like using below regex

  '/[?][a-z^A-Z]+[=]["]/'

and replace my string by just remove the double quote (") in it, expected output is shown below

?a=text1
?b=text23

Please help in solving the above issue using php.

4
  • php.net/manual/en/function.preg-replace.php Commented Sep 27, 2017 at 15:15
  • I need to retain the same found string just to replace the double quote Commented Sep 27, 2017 at 15:17
  • = and " are not reserved characters in regex, the don't need to be character classed. Commented Sep 27, 2017 at 15:20
  • If the goal is to just remove whitespace and double quotes str_replace and trim are probably easier. Commented Sep 27, 2017 at 15:24

2 Answers 2

3

Capture everything except the quote in a capture group () and replace:

$string = preg_replace('/([?][a-z^A-Z]+[=])["]/', '$1', $string);

But you really don't need all those character classes []:

/(\?[a-z^A-Z]+=)"/
Sign up to request clarification or add additional context in comments.

2 Comments

It's not stated in the question but the first parameter seems to need to be trimmed to 1 character. .e.g bc became b (or it is just a typo)
We'll see, I just saw replace my string by just remove the double quote (") in it
0

I will give another solution because i see the php tag also. So let's say you have these:

$a='"text1';
$b='"text2'; 

if i echo them i get

"text1
"text2

in order to get rid of double quote there is a function trim in php that you can use like that:

echo trim($a,'"');
echo trim($b,'"');

the results will be

text1
text2

I dont think you need regex in this occasion as long as you use php. Php can take care of those small things without bother with complex regex expressions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.