To be more specific, I am trying to achieve this.
For example:
Hello. Welcome to stackoverflow!
This is for testing purpose only.
So let's get started!
Good job. See you tomorrow.
and I want to remove ONE newline each. So the result should be:
Hello. Welcome to stackoverflow!
This is for testing purpose only.
So let's get started!
Good job. See you tomorrow.
but if I search it using \n\n regex and replace all with \n, the result is:
Hello. Welcome to stackoverflow!
This is for testing purpose only.
So let's get started!
Good job. See you tomorrow.
the problem is there're two results that matches that regex between Hello... and ...This so it replace twice there.
What I want done is, I just want to remove ONE line for each, no matter how many lines there are.
Wow should I achieve this?
You can use lookbehind:
(?<=\.|!)\n
and replace by nothing. That will remove just the one \n that follows . or !.
Alternatively you can also use lookahead:
\n(?=\w)
This matches any one \n that is followed by a letter, a number or an underscore.
Either delete all:
(?<!\n)\n
See live demo.
Or if your tool/language doesn't support look-behinds, replace all:
([^\n])\n
with captured group 1, which is $1 or \1 depending on your tool/language.
Ctrl+H
Find what: \R(\R+)
Replace with: $1
CHECK Wrap around
CHECK Regular expression
Replace all
Explanation:
\R # any kind of linebreak (i.e. \r, \n, \r\n)
(\R+) # group 1, 1 or more linebreak
Replacement:
$1 # content of group 1
Screenshot (before):
Screenshot (after):
Related
First of all let me apologize for asking this question again. I tried to find the answer, but drew a blank. I want to switch the order of words in a file such as: "dutch, abe - a blank sheet" to "abe dutch - a blank sheet". I'm using regular expressions and I seem to remember it's something like 1, 3, 2. Anyway, thank you in advance.
If each string is on newline you can try like this:
Find:
^(\w+),\s*(\w+)(.*)
Replace:
\2 \1\3
Demo:
https://regex101.com/r/SpKOHE/3
Regex:
(\w+) Match 1 or as many word characters and capture in group.
\s* Match 0 or as many whitespaces.
(.*) Match evertything till the end of line.
Try the following regex pattern:
^(\w+),?\s+?(\w+)(.*)
The substitution order is:
$2, $1$3
https://regex101.com/r/T3c77x/1
You can use ^([a-zA-Z]+),?\s*?([a-zA-Z]+)(.*)$ and then modify your initial string to produce the output as $2, $1$3.
I don't know which language are you using for regex or I would have written the complete code here for you. But the logic will be similar to above.
demo
I have a tex with 32k lines like this example
A01.2 Some Text
A01.3 some Text
A01.4 3Some Text
A02.0 [some text]
B02.1 Text .05 example
I need to replace white spaces with ';' symbol.
I tried (\S{3}\.\d)(\s) but notepad++ highlights/gets both groupsB02.1 with whitespace.
1st question: how do i disable 1st group, or take only 2nd
2nd question: is there another expression do find only this white space?
Here is the real example:
If you want to replace the whitespace by ;, so this B02.1 will be B02.1; using notepad++; since you're capturing the groups then use $ notation in the replace expression.
Find: (\S{3}\.\d)(\s)
Replace: $1;
$1 is for the first captured group.
Hope it helps,
You disable the first group simply not grouping it:
\S{3}\.\d(\s)
Otherwise, the look-behind may suite your case:
(?<=\S{3}\.\d)(\s)
Use a lookbehind so B02.1 won't get matched:
(?<=\S{3}\.\d)(\s)
I have a situation like
something 'text' something 'moretext'
I forgot to add more spacing the first time I created this file and now on each line I should put some whitespace before the 2nd occurence of ' .
Now I can't build a regex for this.
I know that:
my command should begin with :%s because I want it to be executed on all lines
I should use the {2} operator to pick the 2nd occurence ?
If my regex will match something I can put stuff before the match with &
The main problem for me is how to build a regex to match the second ' using the {} notation, it's frustrating because I don't where it's supposed to be inserted or if I should use the magic or non-magic regex in vim.
The result I'm looking for
something 'text ' something 'moretext'
You can use
:%s:\v(^[^']*'[^']*)':\1 ':
[^'] means everything except '
\1 is a backreference to the first captured group (...)
Basically what your doing here is capturing everything up to the second quote, and replacing the line up to (and including) this quote with what you've captured, a space, and a quote.
{2} doesn't mean "the second match", it means "two matches" so it's completely useless for the task.
You could use a substitution like this one or the one in Robin's answer:
:%s/[^']*'[^']*\zs'/ '
Or you could use something like this:
:g/ '[^']*' /norm 2f'i<space>
Yet another way to do it:
:%s/\v%([^']*\zs\ze'){2}/ /
Note: I am using very magic, \v, to reduce amount of escaping.
This approach uses \zs and \ze to set the start and end of the match . The \zs and \ze get set multiple times because of the quantifier, {2} but each occurrence of the group will change the \zs and \ze positions.
For more help see:
:h /\zs
:h /\v
Of course there is always sed, but the trick is getting the quote escaped correctly.
:%!sed -e 's/'\''/ &/2'
I am trying to craft a regular expression that will match all characters after (but not including) the first space in a string.
Input text:
foo bar bacon
Desired match:
bar bacon
The closest thing I've found so far is:
\s(.*)
However, this matches the first space in addition to "bar bacon", which is undesirable. Any help is appreciated.
You can use a positive lookbehind:
(?<=\s).*
(demo)
Although it looks like you've already put a capturing group around .* in your current regex, so you could just try grabbing that.
I'd prefer to use [[:blank:]] for it as it doesn't match newlines just in case we're targetting mutli's. And it's also compatible to those not supporting \s.
(?<=[[:blank:]]).*
You don't need look behind.
my $str = 'now is the time';
# Non-greedily match up to the first space, and then get everything after in a group.
$str =~ /^.*? +(.+)/;
my $right_of_space = $1; # Keep what is in the group in parens
print "[$right_of_space]\n";
You can also try this
(?s)(?<=\S*\s+).*
or
(?s)\S*\s+(.*)//group 1 has your match
With (?s) . would also match newlines
I am using notepadd++ to search and replace quite a few names.
i need to turn these
Neil Kilkenny
Mateja Kežman
Kim Do-Heon
into this:
Neil,Kilkenny
Mateja,Kežman
Kim,Do-Heon
So far i have got:
search: ([A-Z]+)([a-z]+) ([A-Z]+)([a-z]+)
replace: $1,$2
Problem: My regex will not match names with ž or - in them.
I thought about using . but i don't know how many of these special characters there are or where they will occur.
Try with this :
([^ \n\t\r]+) ([^ \n\t\r]+)
(Don't forget the space after the ^)
I think the only thing you are sure of is that the names will be separated by a space and that the first name comes first. As such, I think using the . will be fine.
([^\s]+) (.+)
-or-
([\S]+) (.+)
(Thanks to #Simon for the second, more readable solution).
This should grab everything before the space and put in a group, and everything after the space(including more spaces) and put that in a group.
I just noticed that OP is not switching the order of the groups, if this is the case a simple find a replace on <space> will work just fine. But the regex provided will allow you to do <last name>, <first name> if desired.
Just as reference, Notepad++ uses PCRE. http://perldoc.perl.org/perlre.html
If these are all the spaces that are left, search for a single space (or [ ]+ if you are worried there might be consecutive spaces) and replace with ,. For the single space option, you don't even need regex mode.
This regex seems to be working(reduced the groups from 4 to 2):
([A-Z]+[a-z]+) ([A-Z]+[a-z]+)
To get the matches use \n instead.
Regxp: ([a-zA-Z]+)\s([a-zA-Z]+)
Replace: \1,\2