How to match any character in regex - 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])*\)

Related

Regex Lookahead and "String End" not working

I would like to match a String with a look-ahead using the following regex: /A20.(?!4)/. This string should match:
A20.1
A20.2
A20.3
A20.41
A20.42
A20.400
...
The only A20* string that should not match is
A20.4
It works fine, expect A20.41 or A20.42.. How can I terminate the regex?
I have tried /A20.(?!4)$/, but it did not work.
You could use negated character class such as [^4], which would mean "match everything except four". But I think you still want to match only digits, so I'd simply use character class [123567890] for that (note that 4 is excluded).
So pattern would be:
A20\.[123567890]
Also, you use . (dot) to match the dot, but dot is special regex character, so you need to escape it to treat it literally: \.
you have to look ahead more. If there is another digit behind the 4 then match it, so:
A20.((?!4)|(?=\d\d))

Regex - Finding fullstops (periods) that aren't followed by a space

I'm trying to create a simple Grammar correction tool.
I want to create a regular expression that finds fullstops (" . ") that are not followed by a space so I can replace that with a fullstop and space.
For e.g. This is a sentence.This is another sentence.
Only the first fullstop in the above example should be matched in the expression.
I've tried /\.[^\s]/g but it returns an additional character after the matched fullstop. I would like to match only the fullstop.
How can I do this?
The negated character class [^\s] in the pattern expects a match (any character except a whitespace character), that is why you have the additional character.
If you want to match the dot only, you could use a negative lookahead to assert what is on the right is not a whitspace char or the end of the string:
\.(?!\s|$)
Regex demo
To not match a dot that is not followed by a whitespace char excluding a newline:
\.(?![^\S\r\n])
Regex demo
You can look for all dots using:
(\.)
This will match all dots on below examples:
This is a sentence.This is another sentence.
i am looking. for dots. . ...
You can add a |$ to seek for end of line, and with a little tweak, you get a regex that match all dots not followed by whitespace nor being on the end of a line:
(\.(?!\ |$))
Note that there's a whitespace as literal here. The "must-work-everywhere" example will be like:
(\.(?![[:space:]]|$))
If not, search on the regex reference on the language you use.

Vim find and replace all occurrences of a regex string

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.

regular expression no characters

I have this regular expression
([A-Z], )*
which should match something like
test, (with a space after the comma)
How to I change the regex expression so that if there are any characters after the space then it doesn't match.
For example if I had:
test, test
I'm looking to do something similar to
([A-Z], ~[A-Z])*
Cheers
Use the following regular expression:
^[A-Za-z]*, $
Explanation:
^ matches the start of the string.
[A-Za-z]* matches 0 or more letters (case-insensitive) -- replace * with + to require 1 or more letters.
, matches a comma followed by a space.
$ matches the end of the string, so if there's anything after the comma and space then the match will fail.
As has been mentioned, you should specify which language you're using when you ask a Regex question, since there are many different varieties that have their own idiosyncrasies.
^([A-Z]+, )?$
The difference between mine and Donut is that he will match , and fail for the empty string, mine will match the empty string and fail for ,. (and that his is more case-insensitive than mine. With mine you'll have to add case-insensitivity to the options of your regex function, but it's like your example)
I am not sure which regex engine/language you are using, but there is often something like a negative character groups [^a-z] meaning "everything other than a character".

Regex help NOT a-z or 0-9

I need a regex to find all chars that are NOT a-z or 0-9
I don't know the syntax for the NOT operator in regex.
I want the regex to be NOT [a-z, A-Z, 0-9].
Thanks in advance!
It's ^. Your regex should use [^a-zA-Z0-9]. Beware: this character class may have unexpected behavior with non-ascii locales. For instance, this would match é.
Edited
If the regexes are perl-compatible (PCRE), you can use \s to match all whitespace. This expands to include spaces and other whitespace characters. If they're posix-compatible, use [:space:] character class (like so: [^a-zA-Z0-9[:space:]]). I would recommend using [:alnum:] instead of a-zA-Z0-9.
If you want to match the end of a line, you should include a $ at the end. Turning on multiline mode is only when your match should extend across multiple lines, and it reduces performance for larger files since more must be read into memory.
Why don't you include a copy of sample input, the text you want to match, and the program you are using to do so?
It's pretty simple; you just add ^ at the beginning of a character set to negate that character set.
For example, the following pattern will match everything that's not in that character set -- i.e., not a lowercase ASCII character or a digit:
[^a-z0-9]
As a side note, some of the more helpful Regular Expression resources I've found have been this site and this cheat sheet (C# specific).
Put at ^ at the begining of your character class expression: [^a-z0-9]
At start [^a-zA-Z0-9]
for condition;
pre_match();
pre_replace();
ergi();
try this
You can also use \W it's a shorthand for non-word character (equal to [^a-zA-Z0-9_])