Regex ignoring hyphens and double hyphens - regex

I am trying to get beyond compare ignore a single "-" and double "--" hyphen. I am comparing two PDF documents where the original has a single hyphen and the edited copy replaced all the single hyphens with doubles. I want Beyond Compare to ignore that difference.
I have searched and found where people are able to ignore after either a char or digit (below), but these may be preceded with a space?
[0-9,-]+

You could use the replacement setting (Session->Session Settings)
To do this, you don't need a regex, the replacement is done only in one side.

Related

Regex to prevent double byte characters. Only allow ascii in a range

I have a regex for password control. It will only allow alphanumeric characters and some symbols.
"^(?:(?=.*[a-z])(?=.*[#?!$%^&-*#0-9])(?=.*[A-Z]).{8,128})$$"
Now I want to prevent any double byte characters being used in the password.
I've tried having a list that only allows ascii but that didnt work ^[ -ࣿ]+$
Tried having a list in a range ?=.*[0x21-0x7e]
Would it be possible to flip the logic on my first regex so that if it was a character NOT included in the list it wouldn't allow entry?
I have discovered if I use only double byte it doesn't allow entry but it will if I include a single double byte. I don't want any.
This is in a finnicky scripting language nsis so cant rely on many functions just pattern matching .
The dot allows any character, so replace it with an ASCII range [!-z]:
^(?=.*[a-z])(?=.*[#?!$%^&*#0-9-])(?=.*[A-Z])[!-z]{8,128}$
Note that the backtick is included in this range, and tilda ~ is excluded.
To exclude backtick use [!-_a-z].
To include tilda, use [!-~].
To do both, use [!-_a-~].
Bug in symbol character class fixed: [#?!$%^&-*#0-9] should be [#?!$%^&*#0-9-]: you don’t want the range & to *, either escape the dash or put it first or last.
Unnecessary group removed.

PowerShell RegEx with multiple options

Given a file name of 22-PLUMB-CLR-RECTANGULAR.0001.rfa I need a RegEx to match it. Basically it's any possible characters, then . and 4 digits and one of four possible file extensions.
I tried ^.?\.\d{4}\.(rvt|rfa|rte|rft)$ , which I would have thought would be correct, but I guess my RegEx understanding has not progressed as much as I thought/hoped. Now, .?\.\d{4}\.(rvt|rfa|rte|rft)$ does work and the ONLY difference is that I am not specifying the start of the string with ^. In a different situation where the file name is always in the form journal.####.txt I used ^journal\.\d{4}\.txt$ and it matched journal.0001.txt like a champ. Obviously when I am specifying a specific string, not any characters with .? is the difference, but I don't understand the WHY.
That never matches the mentioned string since ^.? means match beginning of input string then one optional single character. Then it looks for a sequence of dots and digits and nothing's there. Because we didn't yet pass the first character.
Why does it work without ^? Because without ^ it is allowed to go through all characters to find a match and it stops right before R and continues matching up to the end.
That's fine but with first approach it should be ^.*. Kleene star matches every thing greedily then backtracks but ? is the operator here which makes preceding pattern optional. That means one character, not many characters.

Remove space from regular expression

I do not want to show error when adding space. While using this regular expression, when adding space, shows error. How can I solve this?
return /^[A-Za-z\‘“:(),.$#!?=-_/\{}<>%^*]+$/.test(value);
I assume you mean that you want to allow spaces within value, right?
Then simply add a space to the character class. Make sure that the dash is never between two characters in the class, unless you want to specify a range (as you probably meant to do in A-Z but not in =-_). Also, this regex contains an error because you didn't escape the /:
return /^[- A-Za-z‘“:(),.$#!?=_\/{}<>%^*]+$/.test(value);
Add spaces using \s to the regex:
/^[-\sA-Za-z\‘“:(),.$#!?=_\/\{}<>%^*]+$/
As #Tim suggested below, / should be escaped and the - should not come between two characters for which it will define a range. Answer was updated respectfully.
Second, as was noted in the comments below, it's worth mentioning that \s matches all types of spaces (tabs, newlines etc) which is even more general than what the OP asked for

Regex for Alphanumeric characters, .#&,’()+/: and one hyphen only

I have a regex for matching letters, numbers and some special characters as follows: ^[A-za-z0-9 .#&,’()+/:]*$
I need to add a single hyphen to this list, not allowing multiple hyphens, but I'm not quite sure how to do it. I saw something along the lines of -{1} but I don't know how to add that to the existing rexex.
I'm using C++ and Qt5.
How about:
^[A-za-z0-9 .#&,’()+/:]*-?[A-za-z0-9 .#&,’()+/:]*$
that could be reduce to:
^[\w .#&,’()+/:]*-?[\w .#&,’()+/:]*$
I don't know if C++ support it, but it could be reduced to:
^([\w .#&,’()+/:])*-?(?1)*$
^[A-za-z0-9.#&,’()+/:]*-[A-za-z0-9.#&,’()+/:]*$ allows a single hyphen anywhere in the string.
Note that the hyphen may come at any part (at the beginning or end of the string also) and it is mandatory also.
To make the hyphen optional, use ^[A-za-z0-9.#&,’()+/:]*-?[A-za-z0-9.#&,’()+/:]*$

Regex to match non integers?

Trying to create a regex that ignores a proper integer (1, 5, 999, etc.) and forward slashes (/), but finds a match in everything else. For example, it would find a match the following:
test
test1
test-1
but ignores
1
55
7
This is for a mod rewrite.
[^-0-9\/]+ should do the trick, I think. It'll match any string that contains a non-digit.
Edited to add minus sign which would also be allowed in an integer, and then to include the forward slashes mentioned in the question.
This is a very old question, but I noticed that the currently accepted answer ([^-0-9\/]) will not match any part of a number string that has a dash/minus (-) in the middle or at the end or purely consists of dashes.
So the regex will not find a match in strings like 12-34, 1234-, --12, -, or -----, even though these are clearly not valid integer numbers and should thus be caught.
To include these in the regex, you can use something like this:
[^-0-9]+|[0-9]+(?=-)|^-$|-{2,}
This will match
either any part of a string that contains a non-digit, non-minus character (as does the regex from the currently accepted answer)
or any number that is followed by a minus sign - (using a positive lookahead, taking care of the numbers with dashes anywhere but the beginning)
or any string that consists of one single dash
or any part of a string with two or more consecutive dashes
This will thus find a match in any string that is not a positive or negative integer, see also https://regex101.com/r/8zJwCy/1
To also include (or rather exclude) slashes, as the OP requested, they can be added to the first character group.
[^-0-9\/]+|[0-9]+(?=-)|^-$|-{2,}
Note that this will also not match pure series of slashes (/, //, ///,...) and multiple slashes between integers (1//2, 1//-2, //1, 2//) which may or may not be desired (the currently accepted answer behaves identically), see https://regex101.com/r/8zJwCy/3
To also catch these, add another two optional groups that match series of slashes, analogous to the last two ones that take care of the dashes. Or, if you want a single individual slash (/) to be valid, just add one group analogous to the last one, see https://regex101.com/r/8zJwCy/4
[^-0-9\/]+|[0-9]+(?=-)|^-$|-{2,}|\/{2,}
This would match line by line - is that what you need?
^[0-9\/]*$
This should do the job!
^[0-9\/]+$