Regex for placing the percentage symbol before digits
Thread poster: beronike
beronike
beronike
Spain
Local time: 20:08
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: 14:08
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: 20:08
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: 21:08
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: 21:08
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: 20:08
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






TM-Town
Manage your TMs and Terms ... and boost your translation business

Are you ready for something fresh in the industry? TM-Town is a unique new site for you -- the freelance translator -- to store, manage and share translation memories (TMs) and glossaries...and potentially meet new clients on the basis of your prior work.

More info »
Wordfast Pro
Translation Memory Software for Any Platform

Exclusive discount for ProZ.com users! Save over 13% when purchasing Wordfast Pro through ProZ.com. Wordfast is the world's #1 provider of platform-independent Translation Memory software. Consistently ranked the most user-friendly and highest value

Buy now! »