Vim find and replace all occurrences of a regex string - regex

I have a file with a few thousand lines, and in this file I have some regex expressions...
The regex expressions are all over the place, and recently we've changed the expression and I need to update all of them.
I need to change all instances of: [A-z -']+ with [A-z \-']+
so I tried doing :%s/[A-z -']+/[A-z \-']+/g but that replaced all occurrences of [A-z -']+ with [A-z -'[A-z -']+
Is there some other way to do this?

You may use this substitution:
%s/\[A-z -']/[a-zA-Z '-]/g
It is wrong to use [A-z] as it will match many more characters than just [A-Za-z] and it is better to move hyphen to end position before closing ] to get the regex right.

Related

Notepad++ Regex Find all endline without periods

I'm trying to find all lines without ending period (dot) but without finding blank (empty) lines. And after that I want to add ending period to that sentence.
Example:
The good is whatever stops such things from happening.
Meaning as the Higher Good
It was from this that I drew my fundamental moral conclusions.
I have tried few regex but they also find empty lines as well.
Is there a regex for Notepad++ that can achieve that?
Enable Regular Expression match, then search for:
\S(?<!\.)\K\s*$
and replace with:
.$0
Breakdown:
\S Match a non-whitespace character
(?<!\.) It shouldn't be a period
\K Reset match
\s* Match optional whitespace characters
$ End of line
You could use something like this to find the lines that you are interested in adding capture group to it and appending you needed chars.
(?<!\.)\r\n
This works by using negative look behind (?<!\.) to check that there is no . before \r
There is a group or regex operators that can be used to accomplish this type of tasks.
Look ahead positive (?=)
Look ahead negative (?!)
Look behind positive (?<=)
Look behind negative (?
Try this short and effective solution too.
Search: \w$
Replace: $0.

How do I match these text lines in regex?

I'm trying to match the three first text lines in regex, i.e. the ones ending with form.
value="something form"
value="Second cool form"
value="another silly old form"
value="blabla"
How can I do that?
I don't know what tool you are using, but the following pattern should match the first three lines:
.*form"$
Demo
You could simply use:
.*form"$
In order to work, you would have to turn on multiline mode.
Dot (.) means - match me anything but newline character, asterisk (*) means - match me dot zero or more times after which comes text form. Dollar sign ($) is anchor to the string ending.
Take a look at demo. You should learn more about regular expressions here, this is basic regex matching.
You can try using this:
\w*form\b
\w*: Allows characters in front of form
\b: Makes sure that form is at the end of the string.
Regex 101 demo
Actually if you want to match the 'form' as a separate word, you need something like this:
\Wform\W
\W (capital W) is any character which does not represent a word character, at least in perl-like regex.

trying to find the correct regular expression

I have the following cases that should match with a regular expression, I've tried several combinations and have read a lot of answers but still no clue on how to solve it.
the rule is, find any combination of . inside a quoted string, atm I have the following regexp
\"\w*((..)|(.))\w*\"
that covers most of the cases:
mmmas"A.F"asdaAA
196.34.45.."asd."#
".add"
sss"a.aa"sss
".."
"a.."
"a..a"
"..A"
but still having problems with this one:
"WERA.HJJ..J"
I've been testing the regpexp in the http://regexr.com/ site
I will really appreciate any help on this
Change your regex to
\"\w*(\.+\w*)+\"
Update: escape . to match the dot and not any character
demo
From the question, it seems that you need to find every occurrence of one or more dot (along with optional word characters) inside a pair of quotes. The following regex would do this:
\"\w*(\.+\w*)+\"
In "WERA.HJJ..J", you have some word characters followed by a dot which is followed by a sequence of word characters again followed by dot and word characters. Your regex would match one or two dots with a pair of optional word character blocks on either sides only.
The dots in the regex are escaped to avoid them being matched against any character, since it is a metacharacter.
Check here.

What regular expression will select all lines that have more than one punctuation mark?

I have this regular expression:
\..*?\.
But it only selects between two periods, not every punctuation mark, and it also selects across multiple lines.
Would modifying this expression to only take in one line at a time work somehow, if there's also a way to group punctuation into where we have a period?
Just to make things simpler, at this time I only need the expression to recognize periods, exclamation points, and question marks. I don't need it to register commas.
Thanks to Nathan and Agumander below, I know to substitute [.!?] in place of \. now, but I'm still having trouble with the other half of my question.
Just to make sure I'm being more clear, using [.!?].*?[.!?]\s will highlight text between punctuation marks, but across multiple lines. So I can't use it to bookmark only the lines that have multiple punctuation marks.
Placing characters inside a pair of square brackets will match to any of the enclosed characters. In your case you'd want [.?!]
If you want to match any sentence that has two of these, then you'll be looking for a pair of [.!?] separated by zero or more of any character.
The regex that matches strings with more than one of the set [.?!] would then be [.!?].*[.!?]
To make . match newlines, you'd add the s modifier to your regex.
...so the full regex would be /[.!?].*[.!?]/s
Ok I figured it out. Thanks to Agumander and Nathan above I substituted [.!?] in for the two \. in my original regex:
\..*?\. became [.!?].*[.!?]
Putting \s at the end of the regex made it pink select the entire document in notepad++.
The last issue I had was remembering to turn off "matches newline."
Agumander, I think you're asking for a regex that basically finds multiple punctuation marks on a single line. So here's one way to do it.
Here's the text I'm going to match. The regex will match the first line in it's entirety, but will not match the second.
Here's a line with multiple punctuation. The entire line will match the regex!
This line does not have multiple punctuation.
Regex
^.*(?:[\.?!].*){2,}$
Explanation
^ -- Start matching at the beginning of a line
.* -- match any character 0 or more times
(?: -- start a new non-capturing group
[.?!] -- find a character matching a period, question mark, or exclamation point.
.* -- match any character 0 or more times
)
{2,} -- repeat the previous group 2 or more times. This is how we ensure there's at least two punctuation marks before considering it a match.
$ -- end of line anchor, basically stop matching at the end of a line

How to match any character in regex

How can I match all characters including new line with a regex.
I am trying to match all characters between brackets "()". I don't want to activate Dot matches all.
I tried
\([.\n\r]*\)
But it doesn't work.
(.*\) This doesn't work if there is an new line between the brackets.
I have been using http://regexpal.com/ to test my regular expressions. Tell me if you know something better.
I'd usually use something like \([\S\s]*\) in this situation.
The [\S\s] will match any whitespace or non-whitespace character.
The first example doesn't work because inside a character class the dot is treated literally (Matches the . character instead of all characters).
\((.|[\n\r])*\)