Regex match cannot get to work - regex

I've spent far to long trying to get this to work. I'm trying to validation only for the following:
alpha numerics, hyphens, full stops, exclamation marks, open/closing brackets (normal not curly), forward slashes and question marks.
I thought it was the following regex
/([a-zA-Z0-9\!\(\)\-\/\.\?\s])+/
Which kinda works as if I put #~ in the box, it shows that it is invalid. However if I put #~Paul then it states that the match is valid true. It seems to say true as soon as it finds a valid character. The example #~Paul should be false as it contains invalid characters. Should only be true if all characters are valid ones.
Example of working matches should be.
Paul!!
Paul (Stack-Overflow.)!
I'm sure some whizz can help me out there. Please help.

^([a-zA-Z0-9\!\(\)\-\/\.\?\s])+$
What you need are anchors to make validation strict.

Related

Regex to match exact string

I am having some trouble to figure out a way to detect a prefix on a message by having the exact amount of characters, for example:
I tried the regex ^[!]{1} the messages I was testing with were:
!test
!!!test
But that regex would mark both strings as starting with the prefix.
I have saved both prefixes and I want to detect which one is being parsed.
You need to tell the regex what an invalid match is as well. One way to do this is to look for an exclamation mark and then anything but an exclamation mark.
^![^!]
or you could look for an ! then an alpha character:
^![a-zA-Z]
depends what should follow.

Regex for fixing YAML strings

I am trying to create a bunch of YAML files, mostly composed of strings of text. Now when using apostrophes in words, they must be escaped by typing a double apostrophe, because I’m using apostrophes to wrap the strings.
I want to create a regex that will check for apostrophes in the text that aren’t double. What I have is this:
^([^'\n]*?)'(([^'\n]*?)'(?!')([^'\n]+?))*?'$\n
https://regex101.com/r/v4nUTn/3
My issue is that as soon as my string has a double apostrophe, but also has an apostrophe which isn’t a double apostrophe, it doesn’t match because my negative lookahead doesn’t match as soon as it sees the double apostrophe. (for example the string t''e'st won’t match even though it is missing a double apostrophe after the e)
How can I make it so that my negative lookahead will not fail as soon as it sees one double apostrophe?
This regex should work:
\w'\w
Test here.
My guess is that maybe an expression similar to
('[^'\r\n]*'|[^\r\n\w']+)|([\w']*)
would be an option to look into.
If the second capturing group returns true, then the string is undesired.
If you wish to explore/simplify/modify the expression, it's been
explained on the top right panel of
regex101.com. If you'd like, you
can also watch in this
link, how it would match
against some sample inputs.
One suggestion would be to do this in two steps.
For example, if every 'candidate' value looks like this: - 'something here' (where you want to test the apostrophes in the something here content of the string, then first isolate out that content via:
/^\s*- '(.+)'$/im
And then make sure all apostrophe's appear as you want them to appear within match group 1 of the result.
Then, replace the original match with your 'sanitised' match.
Doing this means you don't have to be concerned with the bounding apostrophes causing complications to the check for apostrophes in the value.
Note: there may well be a perfect one-step regex to do this, but understanding that you can break tasks into several steps is useful if you spend a lot of time with regular expressions, and can help you sidestep 'perfect regex paralysis'.
If you want your string to match if there is at least one 'single quote' between your singlequote strings, then you should allow consumption of either a string which doesn't have any singlequote in it or consume if it contains two singlequotes and then you should modify your regex a bit to consume two singlequotes and add |'' in your regex, which will now consume either non-singlequote text or a portion which has at least two singlequotes.
Try this updated regex demo and see if this works like you wanted?
https://regex101.com/r/v4nUTn/4

Regex Match between brackets (...)

I'm trying to grab 2 items from a simple line.
[Title](Description)
EDIT: actually a url looking to display called it description because i want it displayed not actually parsed.
[Trivium](https://www.youtube.com/user/trivium)
Grabbing between the brackets (...) doesn't seem to work at all for me. I've googled and found several variations with no luck, Thanks in advance :)
EDIT:
Tried the following:
[(.+?)]\((.*)\)
[(.+?)]\([^\(\r\n]*\)
[(.+?)]((.+?))
and a cpl more I cant find again
The first regex you listed almost has it right. Try using this regex instead:
\[.+?\]\((.*)\)
As #PM 77-1 pointed out, you need to escape the brackets by placing a backslash in front of them. The reason for this is that brackets are special regex metacharacters, or characters which have a special meaning. Brackets tell the regex engine to look for classes of characters contained inside of it.
Your original regex [(.+?)]\((.*)\) is actually doing this:
[(.+?)] match a period '.' 1 or more times
\((.*)\) match (anything), i.e. anything contained in parentheses
So this regex would match .....(stuff) but would not match [Title](Description), the latter which is what you really want.
Here is a link where you can test out the working regex:
Regex 101

What is wrong with my simple regex that accepts empty strings and apartment numbers?

So I wanted to limit a textbox which contains an apartment number which is optional.
Here is the regex in question:
([0-9]{1,4}[A-Z]?)|([A-Z])|(^$)
Simple enough eh?
I'm using these tools to test my regex:
Regex Analyzer
Regex Validator
Here are the expected results:
Valid
"1234A"
"Z"
"(Empty string)"
Invalid
"A1234"
"fhfdsahds527523832dvhsfdg"
Obviously if I'm here, the invalid ones are accepted by the regex. The goal of this regex is accept either 1 to 4 numbers with an optional letter, or a single letter or an empty string.
I just can't seem to figure out what's not working, I mean it is a simple enough regex we have here. I'm probably missing something as I'm not very good with regexes, but this syntax seems ok to my eyes. Hopefully someone here can point to my error.
Thanks for all help, it is greatly appreciated.
You need to use the ^ and $ anchors for your first two options as well. Also you can include the second option into the first one (which immediately matches the third variant as well):
^[0-9]{0,4}[A-Z]?$
Without the anchors your regular expression matches because it will just pick a single letter from anywhere within your string.
Depending on the language, you can also use a negative look ahead.
^[0-9]{0,4}[A-Za-z](?!.*[0-9])
Breakdown:
^[0-9]{0,4} = This look for any number 0 through 4 times at the beginning of the string
[A-Za-z] = This look for any characters (Both cases)
(?!.*[0-9]) = This will only allow the letters if there are no numbers anywhere after the letter.
I haven't quite figured out how to validate against a null character, but that might be easier done using tools from whatever language you are using. Something along this logic:
if String Doesn't equal $null Then check the Rexex
Something along those lines, just adjusted for however you would do it in your language.
I used RegEx Skinner to validate the answers.
Edit: Fixed error from comments

Floating Point - Regular expression

I am struggling to understand this simple regular expression. I have the following attempt:
[0-9]*\.?[0-9]*
I understand this as zero-to-many numeric digits, followed by one-to-zero periods and finally ending in zero-to-many numeric digits.
I am not want to match anything other than exactly as above. I do not want positive/negative support or any other special support types. However, for some reason, the above also matches what appear to be random characters. All of the following for whatever reason match:
f32
32a
32-
=33
In an answer, I am looking for:
An explanation of why my regular expression does not work.
A working version with an explanation of why it does work.
Edit: Due to what seems to be causing trouble, I have added the "QT" tag, that is the environment I am working with.
Edit: Due to continued confusion, I am going to add a bit of code. I am starting to think I am either misusing QT, or QT has a problem:
void subclassedQDialog::setupTxtFilters()
{
QRegExp numbers("^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$");
txtToFilter->setValidator(new QRegExpValidator(numbers,this));
}
This is from within a subclassed QDialog. txtToFilter is a QLineEdit. I can provide more code if someone can suggest what may be relevant. While the expression above is not the original, it is one of the ones from comments below and also fails in the same way.
Your problem is you haven't escaped the \ properly, you need to put \\. Otherwise the C++ compiler will strip out the \ (at least gcc does this, with a warning) and the regex engine will treat the . as any character.
Put ^ at the start and $ at the end. This anchors your regex to the start and end of the string.
Your expression find a match in the middle of the string. If you add anchors to the beginning and to the end of your expression, the strings from your list will be ignored. Your expression would match empty strings, but that't the price you pay for being able to match .99 and 99. strings.
^[0-9]*\.?[0-9]*$
A better choice would be
^[0-9]*(\.[0-9]+)?$
because it would match the decimal point only if at least one digit is present after it.
One of them needs to be a + instead of *. Do you want to allow ".9" to be valid, or will you require the leading 0?