Regex for placing the percentage symbol before digits
Thread poster: beronike
beronike
beronike
Spain
Local time: 14:36
English to Spanish
+ ...
Sep 17, 2021

Hello,
I am trying to convert digits in this format 9,8% or 45% into (% 9,8) and (% 45) respectively, parentheses included.

In the Regex Assistant, I type:
Find what: ([0-9]+\,?[0-9]*|\.[0-9])%
Replace with: % (([0-9]+\,?[0-9]*|\.[0-9]))

The digits are detected, but instead of replacing by the correct numbers, they are replaced literally with "% ([0-9]+\,?[0-9]*|\.[0-9])"



Does anyone know what I am doing wrong?

<
... See more
Hello,
I am trying to convert digits in this format 9,8% or 45% into (% 9,8) and (% 45) respectively, parentheses included.

In the Regex Assistant, I type:
Find what: ([0-9]+\,?[0-9]*|\.[0-9])%
Replace with: % (([0-9]+\,?[0-9]*|\.[0-9]))

The digits are detected, but instead of replacing by the correct numbers, they are replaced literally with "% ([0-9]+\,?[0-9]*|\.[0-9])"



Does anyone know what I am doing wrong?


I want to use this feature for the auto fragment assembly, so the assembled fragments take into account the position change of the percentage symbol. Despite I am checking the help website, I have no idea how to make it work.
Thanks in advance

[Editado a las 2021-09-17 16:08 GMT]
Collapse


 
William Tierney
William Tierney  Identity Verified
United States
Local time: 08:36
Member (2002)
Arabic to English
Try Regex Assistant Sep 17, 2021

The Regex Assistant (in Find and Replace in MemoQ 9.8) has a Regex Library that includes date conversion. It switches the order of date elements. You could adjust this to switch the order of the percentage signs.

 
Anton Konashenok
Anton Konashenok  Identity Verified
Czech Republic
Local time: 14:36
French to English
+ ...
Don't use regexes in the Replace with: field Sep 17, 2021

Regular expressions should only be used in the Find what: field. In the Replace with: field, use placeholders for the matched substrings: $1, $2 and so on in the order of matching. Since you only have one expression to match, your Replace field should be simply (% $1).

For more details, see "Replacing and reordering" here.


Stepan Konev
 
Stepan Konev
Stepan Konev  Identity Verified
Russian Federation
Local time: 15:36
English to Russian
Try this Sep 17, 2021

Find what:
((\d+,)?\d+)%

Replace with:
(% $1)

Explanation:
\d+ means one or more digits (before the comma).
, means comma itself; if you need to use a period instead, it must be escaped like this: \. to be recognized as a regular period.
? means zero or more times for group 2 that is (\d+,) — this part is needed if you want to convert integer values like 9% into (% 9) too. If you only have decimal numbers to convert, (\d+,\d+)% will suffi
... See more
Find what:
((\d+,)?\d+)%

Replace with:
(% $1)

Explanation:
\d+ means one or more digits (before the comma).
, means comma itself; if you need to use a period instead, it must be escaped like this: \. to be recognized as a regular period.
? means zero or more times for group 2 that is (\d+,) — this part is needed if you want to convert integer values like 9% into (% 9) too. If you only have decimal numbers to convert, (\d+,\d+)% will suffice.
Second \d+ means one or more digits again (after the comma).
Outer parentheses are needed to identify Group 1 that is $1.

To understand which group is 1 or 2 or X..., you can count the opening parentheses from left to right. In this case group 1 is ((\d+,)?\d+)
group 2 is (\d+,)2021-09-18_020741

[Edited at 2021-09-17 23:13 GMT]
Collapse


 
Stepan Konev
Stepan Konev  Identity Verified
Russian Federation
Local time: 15:36
English to Russian
Same effect Sep 18, 2021

Actually, my regex does not change anything except that it is just a bit shorter. I'm sorry.
You regex is just fine. There was no need to reply after Anton Konashenok because his answer is all you need to make your regex work. Forget my previous reply, it contains superfluous information.

[Edited at 2021-09-18 01:44 GMT]


 
beronike
beronike
Spain
Local time: 14:36
English to Spanish
+ ...
TOPIC STARTER
Thank you to everyone Sep 18, 2021

Thank you all for your help.
And there is no need to apologize. Much appreciated your explanations.
I understand a little better now the regexes


 


To report site rules violations or get help, contact a site moderator:


You can also contact site staff by submitting a support request »

Regex for placing the percentage symbol before digits






Anycount & Translation Office 3000
Translation Office 3000

Translation Office 3000 is an advanced accounting tool for freelance translators and small agencies. TO3000 easily and seamlessly integrates with the business life of professional freelance translators.

More info »
Trados Business Manager Lite
Create customer quotes and invoices from within Trados Studio

Trados Business Manager Lite helps to simplify and speed up some of the daily tasks, such as invoicing and reporting, associated with running your freelance translation business.

More info »