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
Related
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):
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 need a regex to replace this text:
("{scriptid}_*0123_00000000_ABC-Description*");
With this text:
("{scriptid}_*0123_00000000_ABC-Description*"));
Or in other words: *I have to add a second closing brace.
Explanation:
("{scriptid}_*0123_00000000_ABC-Description*");
("{scriptid}_4-digits_8-alphanumeric_X-alphanumeric-description");
I tried some expressions but it doesn't really work.
Could some one please help me?
A generic regex that matches all the lines ending with ');' (match also if there are trailing spaces) can be this:
s/\);\s*$/\)\);/
Test online here.
UPDATE: generic regex that adds a closing parenthesis, matches only lines starting with lr_start_transaction:
s/^\s*(lr_start_transaction.*\))\s*;\s*$/\1\);/
Test online here.
Anyway, if we are not talking about generic regex you have to also specify the language you are coding with (cause each regex engine is thinly different).
Consider also to include some lines that does not have to match.
Based on your initial string, you can select it with this:
/(\("\{scriptid\}_[0-9]{4}_[a-z0-9]{8}_[-a-z0-9]+"\));$/i
The $ matches the end of the line, and the i flag is for case-insensitive.
You can then replace with $1); (To simplify the replacement, I left the ; out of the capture group in the match.
http://refiddle.com/refiddles/5625058d75622d65a01c0000
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)
This is a small question, but it has been nagging me for a while. When I do a Find or Find/Replace in Eclipse and I use ^ or $ and check the Regular Expressions box, I always get a message:
String Not Found
I don't understand why. I know about the CTRL+SPACE content assist feature and even when I select ^ from the list, it does not work.
I was looking at this question, Regex to match start of line and end of line for quick find and replace, but it is not working for me. Other regex searches like ^. or ;$ work fine.
I see that ^ and $ are anchor characters, and do not match a string per se, but these characters seem to work in other editors.
EDIT:
For example, I want to convert a list of data into an SQL in clause.
From:
1
2
3
Hi
I need to quote these!
To:
'1',
'2',
'3',
'Hi',
'I need to quote these!',
^ and $ are zero-width anchors as you said, therefore they will not match anything by themselves. You need to use something with the anchors in order to match anything in your file.
I was able to do it with this regex replace:
Find: ^(\s*)(.*)$
Replace: $1'$2',
So what I do is use:
^ to find the beginning of the line.
(\s*) to find and capture any white space.
(.*) to find and capture anything.
$ to find the end of line.
Then replace with
$1 First capture
' quote
$2 Second capture
', quote comma
I wish ^ and $ worked by themselves, but this is cool b/c I can do it in one operation.