Change number size
-
Hi
I am not professinal about regex format. But I would like to do below number to change 3 digit after point.
or I’d like to delete last two or one digit.<Top>1.39514in</Top>
<Left>0.10839in</Left><Top>1.395in</Top>
<Left>0.108in</Left>I can found using “[0-9][0-9][0-9][0-9]in” this regex but I have not found how to change it.
Could you share with me please.
-
If you just want to truncate, and it’s always starting with 3-or-more digits after the decimal point, then it’s relatively easy:
To make sure you’re counting from the decimal point, we’re going to use
\.
to get the literal decimal point (because regex uses.
to match any character.The next important item is the “capture group” using parentheses
(...)
, so that whatever is inside gets saved into memory. In the replacement, use${1}
to use the value from the first capture group,${2}
for the second, and so on…The third important item is using
*
to indicate “0 or more of the previous token”.These three concepts are sufficient for your task:
- Find =
(\.[0-9][0-9][0-9])[0-9]*(in)
- this finds a literal
.
followed by 3 digits, then 0 or more digits then the literalin
, storing the decimal and three digits into group#1 and thein
into group 2 (not necessary, but good practice).
- this finds a literal
- Replace =
${1}${2}
- the first group contained the decimal and 3 digits; the second group contained the
in
, so this means the decimal, three digits, andin
.
- the first group contained the decimal and 3 digits; the second group contained the
However, using the “quantity modifier” aka “multiplying operator”"
{ℕ}
allows you to have ℕ matches of whatever token or subset comes before, which simplifies your FIND expression to- Find =
(\.[0-9]{3})[0-9]*(in)
To simplify again, there is a character escape sequence
\d
which is basically equivalent to[0-9]
for your purposes.- Find =
(\.\d{3})\d*(in)
If you want to do true rounding (if the 4th digit is 5 or higher, add 1, else just truncate), there is no universal regex that will do it; depending on your number of digits, you can craft a regex that will do it, but there is so much to keep track of that you don’t want to do it in regex. If you need rounding, it’s much better to do it in a scripting language like PythonScript: see “Batch Decimal Rounding” discussion and a oneliner PythonScript in the “Rounding numbers pythonscript …” discussion for some solutions.
----
Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the
</>
toolbar button or manual Markdown syntax. To makeregex in red
(and so they keep their special characters like *), use backticks, like`^.*?blah.*?\z`
. Screenshots can be pasted from the clipboard to your post usingCtrl+V
to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries. - Find =