I've got the following text:
get close to,be next to,follow in sequence or along designated route/
direction
How would I match the space after the "e" in close and then replace it with a tab?
Although this may be easy to all of you, I've spent 4 hours trying to do this but haven't been successful.
The general rule would be "match only the space after the second word". I've got over 2000 unique lines which is why I need a regex.
Thank you!!
Search for: ^([^ ]+[ ]+[^ ]+)[ ]
Replace with: \1\t
From the beginning of the line, look for a pattern of non-spaces followed by spaces followed by another set of non-spaces. At the end, match a space character.
The replacement is everything leading up to the final space character, followed by a tab.
Demo: http://regex101.com/r/oS8vV7/1
You may not be able to match only the space you're replacing. That would require a lookbehind of variable length which isn't supported.
In vi editor you can use this search/replacement:
:s/^\([[:blank:]]*\w\+[[:blank:]]\+\w\+\)[[:blank:]]\+/\1\t/
Or shorter (thanks to #PeterRincker):
:%s/\v(\w+\zs\s+){2}/\t
s/\s//2
This can be used to search for a space (\s) and for removing the space, where the 2 represents the second space.
Related
I need help with RegEx I just can't figure it out I need to search for broken Hashtags which have an space.
So the strings are for Example:
#ThisIsaHashtagWith Space
But there could also be the Words "With Space" which I don't want to replace.
So important is that the String starts with "#" then any character and then the words "With Space" which I want to replace to "WithSpace" to repair the Hashtags.
I have a Document with 10k of this broken Hashtags and I'm kind of trying the whole day without success.
I have tried on regex101.com
with following RegEx:
^#+(?:.*?)+(With Space)
Even I think it works on regex101.com it doesn't in Notepad++
Any help is appreciated.
Thanks a lot.
BR
In your current regex you match a # and then any character and in a capturing group match (With Space).
You could change the capturing group to capture the first part of the match.
(#+.*?)With Space
Then you could use that group in the replacement:
$1WithSpace
As an alternative you could first match a single # followed by zero or more times any character non greedy .*? and then use \K to reset the starting point of the reported match.
Then match With Space.
#+(?:.*?)\KWith Space
In the replacement use WithSpace
If you want to match one or more times # you could use a quantifier +. If the match should start at the beginning of string you could use an anchor ^ at the start of the regex.
Try using ^(#.+?)(With\s+Space) for your regex as it also matches multiple spaces and tab characters - if you have multiple rows that you want to affect do gmi for the flags. I just tried it with the following two strings, each on a separate line in Notepad++
#blablaWith Space
#hello###$aWith Space
The replace with value is set to $1WithSpace and I've tried both replaceAll and replace one by one - seems to result in the following.
#blablaWithSpace
#hello###$aWithSpace
Feel free to comment with other strings you want replaced. Also be sure that you have selected the Regular Extension search mode in NPP.
Try this? (#.*)( ).
I tried this in Notepad++ and you should be able to just replace all with $1. Make sure you set the find mode to regular expressions first.
const str = "#ThisIsAHashtagWith Space";
console.log(str.replace(/(#.*)( )/g, "$1"));
I've got a regex expression to replace a dash with a line-break. However, I need to replace the dash only if there is an empty space in front of it.
I've tried this:
var txt = str.replace(/^\s-/g,'<br>');
However, the dash is also replaced if there is no empty space in front of it. Any suggestions what I'm doing wrong?
If not to consider possible awful bugs in the regexp implementation, we should come to the conclusion that your string does match the pattern /^\s-/ if even a dash is placed at the very beginning of a line. How can it happen? Very easy.
Please note that \s stands for a newline (besides other characters). So, if you have a string as "\n-" (newline then dash) it will match the regexp although a dash stands at the beginning of a line (number two) because it also stands exactly one space aside from the beginning of line nuber one.
Thus, try to avoid that a confusing \s, use a mere space or [ \t] instead.
In the line below, I need to these two lines into one single line by replacing the newline and empty space with nothing.
Provisioned Links : 2/14, 2/24, 7/10, 7/12,
7/25, 7/31, 7/32
Therefore I have this regex (in Notepad++):
(\r\n|\n)\s+[0-9]\/[0-9]*
Problem: the match includes the 7/25 - I need it to look for the #/## but not include it.
If I use this lookaround pattern:
(\r\n|\n)\s+(q=[0-9]\/[0-9])*
all lines beginning with newline + spaces are matched, whether or not they end with #/##.
What am I doing wrong?
regex101 fiddle to play with
Be careful:
You should correct the way you constructed the lookahead: (?=....)
Lookarounds are not quantifiable.
so what you need really is [\r\n]\s+(?=[0-9]\/[0-9]*).
Live demo
To normalize whitespace, why not simply replace "comma with additional space after it" with "comma plus one tab character" ?
You don't need that complicated pattern at all, because \s matches spaces, newlines, and tabs all at the same time:
Pattern: ,\s*
Replacement string: ,\t
https://regex101.com/r/T0QJnq/1
I am working on a simple text file in vim where I want to end every sentence with 2 spaces after full stop (dot/period). However, I do not want those sentences which already have 2 spaces after full stop to have further increase in spaces. The test text could be:
This sentence has only 1 space after it. This one has two. This line has again 1 space only. This is last line.
I tried:
%s/\. /\. /g
but this increases all spaces by one. I tried following also but it does not work:
%s/\. \\([^ ]\\)/. \\1/g
How can I achieve this in vim?
Replaces all periods followed by spaces with a period followed by 2 spaces
%s/\. \+/. /g
well, you shouldn't double the escapes, and it works:
:%s/\. \([^ ]\)/. \1/g
result:
This sentence has only 1 space after it. This one has two. This line has again 1 space only. This is last line.~
You may use
%s/\. \( \)\#!/. /g
The \. \( \)\#! pattern matches a . and a space that is not followed with another space.
The (...)#! is a negative lookahead construct in Vim. See Lookbehind / Lookahead Regex in Vim. In other common regex flavors, it is written as (?!pattern). You may learn more about how negative lookaheads work in this answer of mine.
To match any whitespace, replace the literal space with \s inside the pattern.
Adds an extra space after periods followed by exactly one space
:%s/\. \zs\ze[^ ]/ /g
Here is another possibility:
:%s/\. \?/. /g
The \? will capture a space (and only one) if it can, effectively not changing anything if there are already two spaces.
I'm trying to write a regular expression (inside a Google Spreadsheet) to remove parenthesis, the text inside the parenthesis, and space before the parenthesis. Or in other words, I'm trying to extract only the name inside of the text. For example, I'd like the string "A.J. Smith (iOS Developer, San Francisco)" to become "A.J. Smith"
So far I've gotten both =REGEXEXTRACT(D2,"[^()]*") and =REGEXEXTRACT(D2,"^[^(]+") to extract "A.J. Smith " but it leaves that last space at the end. This is probably a really easy problem to solve, I'm just not great with regex.
Just use word boundary.
=REGEXEXTRACT(D2,"^[^(]+\\b")
^[^(]+ greedily matches all the characters upto the first ( symbol including the space which exists before (. Then it backtracks to the last word boundary appears on the matched string because of \b present in the regex.
DEMO
Try this instead:
=REGEXREPLACE(D2,"\s\(.*","")
What I'm doing is replacing everything from a space next to a parenthesis to the end of the string with nothing.
I used https://regoio.herokuapp.com/ to help build a regex to match. This regex would match this example without the space. ^(.+)\s\(
The regex works like this, The ^ matches the beginning of the string, the parenthesis captures whatever expression is inside that you want to use. in this case .+ which matches any character 1 or more times. The \s matchs a whitespace character and \( matches the opening parenthesis.
If you want a regex that removes whitespace at the beginning of the string and any before the parenthesis this should work: ^[\s]*(.+)[\s]+\(
With this regex you can extract all the text you wanted in a single REGEXEXTRACT instead of using multiple ones:
=REGEXEXTRACT(D2,"^[\s]*(.+)[\s]+\(")
I found that =REGEXEXTRACT(D2,"(.*)\s\(") also worked for me.
This should work to remove all parentheses and white space before:
=REGEXTRACT(D2,"\s|\(|\)|\[|]|{|}|")
Feel free to play around with this on rubular.