I'm trying to change in an url all text longer then 6 characters to 6characters~1 p.e.
c:\program files\vim directory\vim73\ to
c:\progra~1\vimdir~1\vim73
I have found a way to find the string length between "\ \"
strlen(matchstr("c:\program files\vim directory\vim73\","\\\zs.*\ze\\"))
but how can I cycle through a string?
How can I cut it to 6 characters (removing the spaces if there are) and put "~1" behind it?
This seems to work for your example text but I'm not sure it would work in all cases (assumes your filename is on it's own line, you could modify it to be more specific otherwise):
:s# ##ge|s#\v\\\zs(\w{6}).{-}\ze\\#\1\~1#&
Edit: just noticed that this leaves a trailing slash. If you want that removed too you can use the following instead:
:s# ##ge|s#\v\\\zs(\w{6}).{-}\ze\\#\1\~1#&|s#\\$##&
I think this regex should do the trick:
:%s /\\\([^\\]\{6\}\)[^\\]*/\\\1\~1\\/g
It essentially means: Match the first 6 characters between two occurences of \ or one ocurrence of \ and newline. Remember those characters (\( ... \) is used to remember) and match everything else until the separator (newline or \). Substitute that with the first 6 characters and ~1.
Related
So I cant use $' variable
But i need to find the pattern that in a file that starts with the string “by: ” followed by any characters , then replace whatever characters comes after “by: ” with an existing string $foo
im using $^I and a while loop since i need to update multiple fields in a file.
I was thinking something along the lines of [s///]
s/(by\:[a-z]+)/$foo/i
I need help. Yes this is an assignment question but im 5 hours and ive lost many brain cells in the process
Some problems with your substitution:
You say you want to match by: (space after colon), but your regex will never match the space.
The pattern [a-z]+ means to match one or more occurrences of letters a to z. But you said you want to match "any characters". That might be zero characters, and it might contain non-letters.
You've replaced the match with $foo, but have lost by:. The entire matched string is replaced with the replacement.
No need to escape : in your pattern.
You're capturing the entire match in parentheses, but not using that anywhere.
I'm assuming you're processing the file line-by line. You want "starts with the string by: followed by any characters". This is the regex:
/^by: .*/
^ matches beginning of line. Then by: matches exactly those characters. . matches any character except for a newline, and * means zero-or more of the preceding item. So .* matches all the rest of the characters on the line.
"replace whatever characters that come after by: with an existing string $foo. I assume you mean the contents of the variable $foo and not the literal characters $foo. This is:
s/^by: .*/by: $foo/;
Since we matched by:, I repeated it in the replacement string because you want to preserve it. $foo will be interpolated in the replacement string.
Another way to write this would be:
s/^(by: ).*/$1$foo/
Here we've captured the text by: in the first set of parentheses. That text will be available in the $1 variable, so we can interpolate that into the replacement string.
I'm really not good with Regex and have been messing about to achieve the following all morning:
I want to find unicode characters ie "\00026" in an SQL string before saving to the database and escape the "\", by replacing it with "\" unless it already has two "\" characters.
\\(?=[0])(?<![\\])
Is what I have written, which as I understand it does:
find the "\" character, positive look ahead for a "0", and look behind to check it isn't preceded by a "\"
But it's not working, so clearly I have misunderstood!
I can shorten it to \\(?=[0])
But then I get the "\" before the 0, even if it is preceded by another "\"
So how do I do:
Replace("\00026", "regex", "\\") to get "\\00026"
AND ensure that
Replace("\\00026", "regex", "\\") also gives "\\00026"
All help much appreciated!
EDIT:
This must parse an entire string and replace all occurrences, not just the first as well - just to be clear. Also I am using VB.net if it makes much difference.
Let me explain why your regex does not work.
\\ - Matches \
(?=[0]) - Checks (not matches) if the next character is 0
(?<![\\]) - Checks (but not matches) if the preceding character (that is \) is not \.
The last condition will always fail the match, as \ is \. So, not much sense, right?
If you want to match / in /000xx whole strings (e.g. separated with spaces), where x is any digit, you can use
\B(?<!/)/(?!/)(?=000\d{2})
See demo (go to Context tab)
To match the string even in context like w/00023, you can remove \B:
(?<!/)/(?!/)(?=000\d{2})
If you do not care about 0s, but just any digits:
(?<!/)/(?!/)(?=\d)
And in case you have \ (not /), just replace / with \\ in the above regular expressions.
You can use the following regex:
(?<!/)/(?=0)
And replace with //
See DEMO
I'm trying to replace multiple spaces with a single one, but at the start of the line.
Example:
___abc___def__
___ghi___jkl__
should turn to
___abc_def__
___ghi_jkl__
Note that I've replaced space with underscore
A simple search using the following pattern:
([^\s])\s+
matches the space at the end of the first line up to the space at the beginning of the next one.
So, if I replace with \1_, I get the following:
___abc_def_ghi_jkl
And that is absolutely not what I expect and regex engines, e.g., PowerGREP or the one in Visual Studio, don't behave that way.
If you want to match only horizontal spaces, use \h:
Find what: (?<=\S)\h+(?=\S)
Replace with: (a space)
There are several possible interpretations of the question. For each of them the replacement will be a single space character.
If spaces is plural and means space characters but not tabs then use
a find string of (^ {2,})|( {2,}$).
If spaces is plural and should includes tabs then use a find string
of (^[ \t]{2,})|([ \t]{2,}$).
If any leading or trailing spaces and tabs (one or more) is to be
replaced with a space then use a find string of (^[ \t]+)|([ \t]+$).
The general form of each of these is (^...)|(...$). The | means an alternation so either the preceding or the following bracketed expression can match. Hence the find what text can match either at the beginning or the end of a line. The ... varies depending on exactly what needs to be matched. Specifying [ \t] means only the two characters space and tab, whereas \s includes the line-end characters.
Ok, so the intention was to replace this:
Hey diddle diddle, \n<br/>
The Cat and the fiddle,\n
with this:
Hey diddle diddle,\n<br/>
The Cat and the fiddle,\n
A slightly modified version of Toto's answer did the trick:
(?<=\S)\h+(?=\S)|\s+$
finding any space(s) between word-characters and trailing space at the end of the line.
How do I replace a word with a new line and a word using regex with an empty string in Powershell?
Below is a sample content... I need to delete all the use database and go I'm using powershell and powershell_ise for editor:
use database_instance
go
if condition
You need to match Newline and also space after the newline:
/use database_\w+\n\s*\w+/g
$sql = #"
use database_instance
go
if condition
"#
$sql -ireplace 'use\s+\w+_\w+\s*(?:\r?\n)+\s*go' , ''
How this Works:
Using -ireplace for case insensitive regex.
Find the word use followed by one or more whitespace \s+ followed by one or more word characters \w+, then an underscore _.
One or more word characters \w+, followed by 0 or more whitespace (just in case)
A non-capturing group (?:) since we don't need the result, this is just to encapsulate a newline that accounts for windows and unix line endings. It consists of an optional CR followed by a LF, and this is matched 1 or more times.
Followed by 0 or more whitespace \s* then the word go.
Replace it with nothing!
This does leave some empty space, but that shouldn't be too big of an issue since the SQL parser won't care.
Note
In your comments you said you tried:
$out -replace "/use database_\w+\n\w+/g"
Be aware that powershell does not use /regexhere/ syntax. The forward slashes are treated as literals, so the flags you specified are as well. The replace is global by default so you don't need g anyway.
I want to remove all the lines until some terminating string. So something like this :
These
lines
|| that can contain numbers 123 or characters like |*{
will be removed
But the following lines
will
remain.
I want to obtain :
But the following lines
will
remain.
I tried searching the regexp /removed$/ and replacing with an empty string, but it says 0 matches.
How can I do this?
Thanks for any help !
Make sure you check ". matches newline", and use .*removed\.$:
Notice that you need .* at the start to match everything up until the terminating string, and you have to escape the literal . at the end.
Also, Notepad++ doesn't want slashes around its regexes.
If you don't have the . at the end of your terminator, just remove the \. from the regex.
To remove the trailing newline as well, you could use .*removed\r?\n?.