2

Here is my string.

$text = 'I am  [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how  are you? sorry [pause ="3000"] i can not hear you ?[rate="-70.00%"] i still can\'t hear you[endrate] ? [rate="+50.00%"]why i can\'t hear you[endrate] ?';

I want to replace [pause = "3000"] by <break time="3000ms">

I have written following regex but it is selecting till the last "]

\[pause.*\"(\d+)\".*\"]

PHP : $text = preg_replace("/\[pause.*\"(\w+)\".*\"]/", '<break time="$1ms"/>', $text);

If I were to find a solution where the regular expression only selects 'any number'

any number"]

my problem would be solved.

But I am not able to find how to do it.

Do you have any suggestions ?

2 Answers 2

2

You may use

\[pause[^]]*"(\d+)"]

Or (if there may be something else after the digits):

\[pause[^]]*"(\d+)"[^]]*]
                   ^^^^^

and replace with <break time="$1ms"/>. See the regex demo

Details

  • \[pause - [pause substring
  • [^]]* - 0+ chars other than ]
  • " - a double quotation mark
  • (\d+) - Group 1: one or more digits
  • "] - a "] substring.

PHP demo:

$str = 'I am  [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how  are you? sorry [pause ="3000"] i can not hear you ?[rate="-70.00%"] i still can\'t hear you[endrate] ? [rate="+50.00%"]why i can\'t hear you[endrate] ?';
echo preg_replace('~\[pause[^]]*"(\d+)"]~', '<break time="$1ms"/>', $str);
// => I am  [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how  are you? sorry <break time="3000ms"/> i can not hear you ?[rate="-70.00%"] i still can't hear you[endrate] ? [rate="+50.00%"]why i can't hear you[endrate] ?
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you much for your the answer.
2

Your regex matches too much due to the last part .*\" If you remove that part you would have your match for the current example data, but the first .* still matches any character including for example characters like "[].

What you could do is replace that first .* by matching the equals sign surrounded by horizonal whitespace character like \h*=\h*

Note that you don't have to escape the double quote.

You might use:

\[pause\h*=\h*"(\d+)"]

Regex demo

That will match

  • \[pause Match [pause
  • \h* Match zero or more horizontal whitespace characters
  • = Match =
  • \h*" Match zero or more horizontal whitespace characters followed by a "
  • (\d+) Capture in a group one or more digits
  • "] Match "]

And replace with:

<break time="$1ms"> or use <break time="$1ms"/>

For example:

$text = 'I am  [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how  are you? sorry [pause ="3000"] i can not hear you ?[rate="-70.00%"] i still can\'t hear you[endrate] ? [rate="+50.00%"]why i can\'t hear you[endrate] ?';
$text = preg_replace('/\[pause\h*=\h*"(\d+)"]/', '<break time="$1ms"/>', $text);
echo $text;

Demo

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.