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
Related
So for example if I have something like:
<House \>
windows
walls
<\House>
and I want to remove lets say walls how do I do that with regex or is there a better option than regex?
So I need it to look like:
<House \>
windows
<\House>
So how would I approach this with regex?
(?s)(?<=<Building \/>).*(?=<\/Building>) Will capture only what is between your two building tags.
(?s) allows . to match newline characters
(?<=<Building \/>) means your content should follow a block of text matching <Building />
.* is your content
(?=<\/Building>) just means that your content should be followed by </Building>
Demo
If you want to trim all that ugly white space, you could always use this pattern ((?s)(?<=<Building \/>)\s*(.*)\s*?(?=<\/Building>)) and refer to capture group 1.
What about:
Find: (<Building />\s+)floor(\s+</Building>)
Replace: \1corner\2
How to find the text between the second and fourth slashes in a path like /folder/subfolder-1/subfolder-2/subfolder-3? I’m trying to replace this with something like /folder/new-folder/subfolder-3.
The most important for me is to be able to find the part after the n-th slash.
I tried the regex /((.*?)/){3}, but it doesn’t work.
Using Match resetter \K meta-character you are able to do it in a simpler way.
Find:
/.*?/\K(.*?/){2}
Replace with:
new-folder/
One way you could to it is by using this string in the pattern to replace
(/.+?)(/.+?){2}(/\S+)
And use this one in your pattern to replace it with
$1/new-folder$3
From your string:
/folder/subfolder-1/subfolder-2/subfolder-3
(/.+?) will match /folder as $1
(/.+?){2} will match /subfolder-1/subfolder-2 as $2 (not used)
(/\S+) will match everything that isn't a space, in this case/subfolder-3 as $3
Leaving you room to insert your new-folder in-between.
How can I just mark till the slash?
Find what: (/[^/]+/)[^/]+/[^/]+
Replace with: $1new-folder
To find text between second and forth slash you can use the regex ^(/[^/]*/)([^/]*/[^/]*) then you can reference to the text between slashes with \2 when replacing the text.
To keep the text before the slashes you can enter something like \1myNewTextBetweenSlashes2and4.
In notepad++ Find by this:
(/[^/]+)(?:/[^/]+/[^/]+/)(.*)
And Replace by this:
\1\/new-folder/\2
Make sure that: .matches newline is not checked
{2} indicates 2 levels after first level will be repalced by new-folder
Find:
(\/.*?\/)(.*?\/){2}(.*)
Replace:
$1new-folder/$3
Demo: https://regex101.com/r/XIA3IN/3
I have a list of data in this format
0000000000000000|000|000|00000|000000|CITY|GA|123456|8001234567
I need to replace the last piece of data with the word N/A so there is no phone number in the list.
0000000000000000|000|000|00000|000000|CITY|GA|123456|N/A
Thank you for the assistance, much appreciated.
The simplest and fastest solution for that would be to search for
[^|\r\n]+$
and replacing all with N/A.
Explanation:
[^|\r\n]+ matches one or more characters except | or newlines, and $ makes sure that the match only occurs at the end of a line.
Do a find/replace, with the mode set to "Regular expression".
Find:
(.*)\|[0-9]*
Replace:
\1|N/A
If your phone numbers contain any non-numeric characters (such as periods, hyphens, spaces, etc.), then I would recommend the following adjustment to the regex given by #Bitwise:
(.*)\|(.*)$
Also, in Notepad++, the backreference syntax is not
\1
but rather
$1
which means your replace string will actually be
$1|N/A
You can use
(?!.*\|)(.+)
to mark the end of the line.
In Notepad++ you can use the search and replace (regex) function.
How to write regex to match if only first character is . ?
I'v been trying this:
hide_file={.*}
But unfortunately, it will find all files that has . in it.
For example:
/home/user
.bashrc
.bash_history
some_text.csv
foo.json
In this example I would like this regex to affect only first two files.
P.S
That's the requirement:
Supported regex syntax is any number of *, ? and unnested {,} operators. Regex matching is only supported on the last component of a path, e.g. a/b/? is supported but a/?/c is not. Example: deny_file={*.mp3,*.mov,.private}
Simply use
^\s*?\..*$
See http://regex101.com/r/oW1xP3 for a live demo
If you are sure there are no whitespaces in front of your input remove the \s*?
The trick is to anchor ^ the regex to the beginning of the string.
^\. will match any string that begins with a period. *Note: * you will need to escape this regex appropriately for your programming language.
hide_file={^\.}
I want to use a regex to replace some strings in my file. I search for:
%s/^ [a-z]*/ /
what I want to do is to replace every [a-z]* that have 2 whitespaces with the sane [a-z] prepended with 4 whitespaces. Is there any "inplace" replacement or how would I reach that with vim?
With best regards
:%s/ \([a-z]*\)/ \1/g
should do the job; beware of running this multiple times, though because the result of the replace will match the input pattern :)
I find it more straightforward to use the \ze object to define the end of the match:
:%s/ \ze[a-z]*/ /g
so the [a-z]* is not included in the replace, but just used to match the relevant spaces.