Windows 10 (64 bit), Emacs 25.1.
Suppose I have text:
MyActivity.java:124: RestClient().getAuth("api/method1" + Integer.toString(id) + ".json")
MyActivity2.java:224 RestClient().getAuth("api/method2" + Integer.toString(id) + ".json")
MyActivity2.java:224: RestClient().getAuth("api/method2" + Integer.toString(id) + ".json")
MyFragment.java:684: RestClient().getAuth("api/method2" + Integer.toString(id) + ".json")
MyActivity2.java:224: RestClient().getAuth("api/method1" + Float.toString(id) + ".xml")
Fragment.java:234: RestClient().getAuth("api/method1" + Integer.toString(id) + ".json")
MyActivity2.java:224: RestClient().getAuth("api/method4" + Float.toString(id) + ".xml")
How I can remove all text except "api/xxx". So result must be:
"api/method1"
"api/method2"
"api/method2"
"api/method2"
"api/method1"
"api/method1"
"api/method4"
I want to use built-in capabilities of Emacs (without write custom elisp script or Emacs macros).
Select the lines you want to modify and run M-xreplace-regexp. Use .*\("api/.+?"\).* for the replace regexp and for the replacement use \1.
This regex says:
.* - match any number of any character
\(...\) - saving the bit between the parens
"api/.+?" - a literal "api/ followed by anything up to and including the next ".
.* - followed by any number of any character
And replace it with \1, which is whatever text was in the first (and only) set of \(\) above.
The output is the expected:
"api/method1"
"api/method2"
"api/method2"
"api/method2"
"api/method1"
"api/method1"
"api/method4"
If you were happy for the results to be output in a separate buffer, you could use occur like so:
C-uM-so "api/.+?" RET
Related
Say I have this Matlab or Octave char variable:
>> filename = 'my.file.ext'
I want a regexprep command that adds a suffix, say '_old', to the file name before the extension, transforming it into 'my.file_old.ext'.
The following replaces all dots with '_old.':
>> regexprep(filename, '\.', '_old.')
ans =
'my_old.file_old.ext'
What is a regexprep command that prepends '_old' only to the last dot? (Ideally, if there is no dot (no extension), append '_old' at the very end.)
Thank you in advance!
If doing it without regular expressions is an option, you can use fileparts as follows:
filename = 'my.file.ext';
suffix = '_old';
[p, n, e] = fileparts(filename); % path, file, extension; each possibly empty
result = [p, n, suffix, e];
Example in Octave.
You may use
regexprep(filename, '^(?:(.*)(\.)|(.*))', '$3$1_old$2')
See the regex demo
Details
^ - start of string
(?:(.*)(\.)|(.*)) - a non-capturing group matching either of the two alternatives:
(.*)(\.) - Group 1 ($1 backreference refers to the value of the group): any zero or more chars as many as possible and then Group 2 ($2): a dot
| - or
(.*) - Group 3 ($3): any zero or more chars as many as possible
If an alternative is not matched, the backreference to the capturing group is an empty string. Thus, if (.*)(\.) matches, the replacement is Group 1 + _old + Group 2 value. Else, it is Group 3 + _old (just appending at the end).
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).
I am on a Windows machine, and I am looking for a way to use Regex to count the number of occurrences of the File.separator characters in a path. Below is my code, and it outputs 0 every time.
var dummyPath:String = "C:" + File.separator + "A" + File.separator + "B.jpg";
var pattern:RegExp = new RegExp(File.separator,"g");
trace(dummyPath.match(pattern).length);
//Outputs 0
I'm not sure what else to do.
I wouldn't use a regex in a case like this, just because they're a lot more confusing to work with (and I think a lot more inefficient as well) than usual string operations, and you aren't doing anything here that's complicated enough to make up for the difference.
In that case, I would just go about it this way:
var dummyPath:String = "C:" + File.separator + "A" + File.separator + "B.jpg";
trace(dummyPath.split(File.separator).length - 1);
As for what you're running into though, remember that operating systems' file separators are generally either / or \. You're saying you're running this on Windows. That means you're passing "\" into the constructor for the regex. \ is used to begin escape sequences in regexes the same way it's used like that in strings.
So essentially you're not describing a regex that looks for instances of "\" on a Windows machine; you're describing a regex that starts an escape sequence and doesn't finish. So to use a regex in this case, you would need to escape \ with another \:
// This is technically untested, but the principle is the same.
var pattern:RegExp = new RegExp(File.separator.replace("\\", "\\\\"), "g");
Its not matching because the file separator you are using is a metacharacter.
The escape \.
The regex engine expects metachars, used as literals, to be escaped.
Try \\, which would be "\\\\" as a double quoted string.
If you run into a forward slash separator, just escape it too, does no harm.
So, concatenate the variable with an escape as a string Sep = "\\" + Sep; or something.
I am using vim to process text like the following
0x8000 INDEX1 ....
0x8080 INDEX2 ....
....
0x8800 INDEXn ....
I want to use regular expression to get the index number of each line. that is
0x8000 ~ 0
0x8080 ~ 1
....
0x8800 ~ n
The math evaluation should be (hex - 0x8000) / 0x80. I am trying to using vim regular expression substitution to get the result in line
%s/^\(\x\+\)/\=printf("%d", submatch(1) - 0x8000)
This will yield
0 INDEX0
128 INDEX1
....
2048 INDEXn
What I want to do is to further change it to
0 INDEX0
1 INDEX1
...
20 INDEXn
That is, I want to further divide the first column with an 0x80. Here is when I get the problem.
The original argument is "submatch(1) - 0x8000". I now add an "/ 0x80" to it, which forms
%s/^\(\x\+\)/\=printf("%d", (submatch(1) - 0x8000)\/0x80)
Now Vim report error
Invalid expression: printf("%d", (submatch(1) - 0x8000)\/0x80))
It looks like vim meet problem when processing "/". I also tried with a single "/" (without escape), but still fails.
Can anyone help me on this?
You can't use the separation character in a sub-replace-expression.
From
:h sub-replace-expression :
Be careful: The separation character must not appear in the expression!
Consider using a character like "#" or ":". There is no problem if the result
of the expression contains the separation character.
Instead, change the separator to no longer match the division operator. E.g., use #.
:%s#^\(0x\x\+\)#\=printf("%d", (submatch(1) - 0x8000)/0x80)
Note that I had to change your regex (specifically ^\(\x\+\) to ^\(0x\x\+\)). I don't know why yours works for you, but from :h character-classes, \x shouldn't include the trailing 0x :
/\x \x \x hex digit: [0-9A-Fa-f]
Also, your regex is a bit easier to read (to me at least) using very-magic mode (see :h magic):
:%s#\v^(0x\x+)#\=printf("%d", (submatch(1) - 0x8000)/0x80)
I have an expression which matches the question mark in a url query string and I find myself needing to extend the expression to accommodate for a case where the URL I am trying to read contains the unicode equivalent of the question mark %3d
the expression is
var regexS = "[\\?&]" + name + "=([^&#]*)";
From what very little I know of RegEx I thought this might work
var regexS = "[\\?&]|[\\%3d&]" + name + "=([^&#]*)";
Thanks for the help
Assuming Javascript for the purposes of string escaping.
In "[\\?&]|[\\%3d&]" + name + "=([^&#]*)" note that
concatenation (abc) has priority over alternation (a|b|c) and
[\\%3d&] means "percent or 3 or d or ampersand" (character class).
The escaped form of ? is %3F, not %3D. %3D means =. See wikipedia: percent encoding
the ampersand in the first character class is present to match &q2= in www.example.com?q1=v1&q2=v2. Perhaps you want to allow escaped ampersand as well. Its escaped form is %26
You probably mean "([\\?&]|\\%3f|\\%26)" + name + "=([^&#]*)" instead.
Also note that ? has no special meaning inside a character class and doesn't need to be escaped: "([?&]|%3f|%26)" + name + "=([^&#]*)"