Vim: Adding * symbol between numbers and the left parenthesis - regex

I have thousands of lines in a file in the following format:
x1(t) = 1.568(1-t) + 5.145(1-t)**2 + ... (other terms)
x2(t) = 3.347(1-t) + 1.304(1-t)**2 + ...
x3(t) = 7.016(1-t) + 1.901(1-t)**2 + ...
x4(t) = 0.843(1-t) + 5.335(1-t)**2 + ...
....
As you can see, there is no * sign between the numbers and the left parenthesis. I could record a macro to fix that ,but for some reason I do like to use the :substitute command with regular expressions, instead.
I've tried the following:
:%s/[0-9]([0-9]/*(/g
But that does substitute also the digits before and after the left parenthesis. I don't know how to match the parenthesis alone without matching the numbers before and after.
I appreciate your help.

You may use
:%s/[0-9]\zs(\ze[0-9]/*(/g
This is roughly equivalent to a [0-9]\K\((?=[0-9]) PCRE regex and matches:
[0-9] - a digit
\zs - omit the text matched so far from match memory buffer
( - a ( char
\ze - end of consuming pattern, the rest is context
[0-9] - a digit must appear after ( (the digit is just context, not part of a match).

Related

regular expression: extract number in an expression

Suppose I have the following expression:
"1+3x+52-9-45x+x"
my goal is to extract all the constants:
[1,+52,-9]
I have tried using Python:
re.findall("[+-]?\d+","1+3x+52-9-45x+x")
Result is:
['1', '+3', '+52', '-9', '-45']
which are not correct because the coefficents of x are also extracted.
I also tried:
re.findall("[+-]?\d+[+-]?","1+3x+52-9-45x+x")
But still not working.
Try this Regex:
(?:[+-])?\b\d+\b
Demo
OR
(?:[+-])?\d+(?=[\s+-]|$)
Demo
Explanation(for the 1st Regex):
(?:[+-]) Matching Either a + or a -(Add more operators if you want)
? Making + or - optional
\b\d+\b matching 1 or more digits between 2 non-words(so it will not include the coefficients)
Explanation(for the 2nd Regex):
(?:[+-]) Matching Either a + or a -(Add more operators if you want)
? Making + or - optional
\d+(?=[+-]) matching 1 or more digits(greedy) immediately followed by a + or - or a space or If it is the end of line. You can add more operators if you want.

can't get specific query-replace-regexp in emacs to work

I have a block of code as such, and I am trying to modify it with query-replace-regexp in emacs.
fz.D(1,:) = Dcen;
fz.vol(1,:) = (4/3)*pi*((fz.D(1,:)/2).^3);
fz.POC(1,:) = Ac*fz.vol(1,:).^bc;
fz.w(1,:) = cw*fz.D(1,:).^eta;
% size - bin edges
fzl.D(1,:) = Dlim;
I want it to look as so:
fz.D(ind,:) = fz.D(1,:);
fz.vol(ind,:) = fz.vol(1,:);
fz.POC(ind,:) = fz.POC(ind,:);
and so fourth.
I tried to enact this change with the following, but it doesn't seem to work
query-replace-regexp
\(*\)(1,:) = *; -> \1(k,:) = \1(1,:);
But that seems to do nothing.
Any suggestions about how I should fix this?
I don't know emacs, but your regular expression needs to use .* for the 'match any length substring' operation:
query-replace-regexp \(.*\)\((1,:)\) = .*; -> \1(ind,:) = \1\2;
(This also makes use of a second \(\) group to avoid repeating the part of the pattern that you want to repeat in the replacement text)
The reason:
In regular expressions, * is a postfix operator that matches "0 or more of the previous item". When found with no previous item, it matches a plain *. Thus, your expression \(*\)(1,:) = *; matched the exact text *(1,:) = followed by 0 or more spaces followed by ;. You want to use .* to "match anything", as this matches 0 or more . items (where . matches any one non-end-of-line character).

How to select a part of a string OR another with REGEXP in MATLAB

I've been trying to solve this problem in the last few days with no success. I have the following string:
comment = '#disabled, Fc = 200Hz'
What I need to do is: if there's the string 'disabled' it needs to be matched. Otherwise I need to match the number that comes before 'Hz'.
The closest solution I found so far was:
regexpi(comment,'\<#disabled\>|\w*Hz\>','match') ;
It will match the word '#disabled' or anything that comes before 'Hz'. Problem is that when it first finds '#disabled#' it copies also the result '200Hz'.
So I'm getting:
ans = '#disabled' '200Hz'
Summing up, I need to select only the 'disabled' part of a string if there is one, otherwise I need to get the number before 'Hz'.
Can someone give me a hand ?
Suppose your input is:
comment = {'#disabled, Fc = 200Hz';
'Fc = 300Hz'}
The regular expression (match disabled if follows # otherwise match digits if they are followed by Hz):
regexp(comment, '(?<=^#)disabled|\d+(?=Hz)','match','once')
Explaining it:
^# - match # at the beginning of the line
(?<=expr)disabled - match disabled if follows expr
expr1 | expr2 - otherwise match expr2
\d+ - match 1 or more digits, equivalently [0-9]+
expr(?=Hz) - match expr only if followed by 'Hz'
Diagram:
Debuggex Demo

Using regular expression to strip out every character that's not in a list

I want to strip out every character that isn't in a list of valid characters.
In this example, I want to strip out everything that's either: (a) not alphanumeric, or (b) is the e accent grave character:
Line = rereplace(Line,'[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + Chr(233) + ']','','all')
I think , I just need a 'not' symbol or something .
You can use shortcuts for most of that:
Line = rereplace(Line,'[^A-Za-z0-9' + Chr(233) + ']','','all')
The ^ inside the bracket means 'not these characters'

Regex help. I need ideas for solve the String Calculator kata with Groovy

I'm working on String Calculator code kata with Groovy.
There are a lot of scenarios that solve for achieve the solution:
I have:
//;\n1;2;3
//#\n1#2#3
//+\n1+2+3
//*\n1*2*3
//?\n1?2?3
I want:
1,2,3
My implementation:
String numbers = "//;\n1;2;3"
numbers.find(/\/\/\S[\n]/) { match ->
def delimeter = match[2]
numbers = numbers.minus(match).replaceAll(delimeter, ",")
}
With this solution I solved the first and second expressions, but I don't know how solve the others expressions.
java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
The problem is that we must also consider any symbol that match with the sintaxt of regular expressions like +, * or ?
Finally I have the solution:
String numbers = "//+\n1+2+3"
numbers.find(/(?s)\/\/(.*)\n/) { match ->
def delimeter = match[1] // also match[0][2]
numbers = numbers.minus(match[0]).replace(delimeter, ",")
}
An important point (?s):
In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.
Dotall mode can also be enabled via the embedded flag expression (?s)
But really the problem was here: .replace(delimeter, ",")
//(.)\n(\d)\1(\d)\1(\d)
Need to use links.
(.) - math thiw any character, and \1 - math thiw character on it\
For next example you can apply this: //\[(.*?)\]\\n(\d)\1(\d)\1(\d)
It math thiw
//[*]\n12**3
And last: //\[(.*?)\]\[(.*?)\]\\n(\d)\1(\d)\2(\d)
//[*][%%]\n1*2%%3
And finaly:
//\[(.*?)\](?:\[(.*?)\])?\\n(\d)\1(\d)(?:\2|\1)(\d)
I think it's can work ewerythere
P.S : (\d) you can replace what you want. I think you need (\d*)