-3

I need to transform text using regex

TPI +2573<br>
NM$ +719<br>
Молоко +801<br>
Прод. жизнь +6.5<br>
Оплод-сть +3.6<br>
Л. отела 6.3/3.9<br>
Вымя +1.48<br>
Ноги +1.61<br>

to this one

<strong>TPI</strong> +2573<br>
<strong>NM$</strong> +719<br>
<strong>Молоко</strong> +801<br>
<strong>Прод. жизнь</strong> +6.5<br>
<strong>Оплод-сть</strong> +3.6<br>
<strong>Л. отела</strong> 6.3/3.9<br>
<strong>Вымя</strong> +1.48<br>
<strong>Ноги</strong> +1.61<br>

Is it possible with regex in PhpStorm's search and replace dialog?

Emma
  • 27,428
  • 11
  • 44
  • 69

1 Answers1

1

Given your text, you can use this regex,

.* +

and replace it with <strong>$0</strong> (Notice there is a space after </strong>)

We're using .* to capture everything but stop just before one (possible one or more) space because that's the point after which we want the text to remain intact. Once we capture the text, we use back-reference $0 to replace the match with <strong>$0</strong> so only matched text is placed within <strong> tags.

Regex Demo

Just in case, if this doesn't work for any of the samples you haven't included in your post, then please list the rules of replacement and I will give you a more robust solution, that will work flawlessly for your given rules.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • Kumar, I've found a bit overcomplicated solution, but it's working: `([А-Яа-я\.]*\s?[A-Za-zА-Яа-я\$\.\-]+)(\s+)([\d\+\.\/]+)(
    )` to this `$1$2$3$4`
    – Amanzhol Medetov May 12 '19 at 06:37
  • @AmanzholMedetov: Oh, I see that, the solution you're using is unnecessarily complicated and may be hard to understand as well as maintain. Isn't my solution working for you? Few things I will suggest in your solution. You don't have to escape special characters like `+` `.` `$` or even `-` while you place them in a character set. Secondly the regex you're using looks character sensitive and isn't a general one. Due to which, say if your string has some more special characters that you haven't placed in char set, will not work then, like try placing a `#` within your data. – Pushpesh Kumar Rajwanshi May 12 '19 at 07:44
  • And you don't need to have group2, group3 and group4 separately and rather can have them in one as `(\s+[\d\+\.\/]+
    )` as all you're doing is replace by `$2$3$4` hence no need to group them separately. And if you really want to continue using the regex you're using, then use this improved form of same regex where I've made it a bit better. [Improved regex](https://regex101.com/r/EGELxA/2/)
    – Pushpesh Kumar Rajwanshi May 12 '19 at 07:59
  • 1
    Tested. Your solution is much more better. Appreciate it. Thanks. – Amanzhol Medetov May 13 '19 at 08:33
  • @AmanzholMedetov: If my solution works well for you, consider accepting it which may help others and will also award you with +2 rep. – Pushpesh Kumar Rajwanshi May 13 '19 at 10:33