Perl Reg Expressions, word anchor and special characters - regex

I am trying to write a script to detect variable types and have been having an issue with my regular expression.
if(/\$\b([a-zA-Z]|_ )(\w)*\b /x && !/ /)
is what I am using to detect a scalar. The problem right now though is that \b \b doesn't seem to be working with the special characters (!##$, etc). For example it would count $var### as a valid name. Any ideas?

The regex you have is correct. all you need to do is to anchor it to the start and end of the string
^\$\b([a-zA-Z]|_ )(\w)*\b$
example http://regex101.com/r/uD1eR7/1
Changes made
^ anchors the regex at the start of the string
$ anchors the regex at the end of the string
Note you can also move the underscore _ into the character class and remove the word boundaries as it does not give extra advantage
^\$[a-zA-Z_]\w*$

Related

Using regular expression to match a row which contains a certain word and is the last line of the string

I would like to find a regular expression that will match a row, which contains a certain word (or character) and is the last line of the string.
Any ideas on how I could do this?
The idea is to use the fact that the dot doesn't match newlines.
You can use this kind of pattern:
.*?TARGET.*$
or to isolate the target:
TARGET(?=.*$)
Notices:
You have to take care that the multiline mode (the m modifier for the most regex engines) isn't activated, otherwise $ will match the end of a line (and not the end of the string in particular).
If available, prefer to use the \z anchor instead of $ because in Perl compatible regex engine, $ succeeds also before a trailing newline sequence (however you can also take advantage of this flexibility):
.*?TARGET.*\z

Regular expression let periods in (.)

My regular expression lets in periods for some reason, how can I keep that from happening.
Rules:
4-15 characters
Any alphanumeric characters
Underscore as long as it's not first or last
[A-Za-z][A-Za-z0-9_]{3,14}
I don't want "bad.example" for work.
Edit: changed to 4-15 characters
Your regex matches example as a substring of bad.example. Use anchors to prevent that:
^[A-Za-z][A-Za-z0-9_]{1,12}[A-Za-z]$
Note that (like your regex) this regex also prevents digits from matching in the first and last position - if they should be allowed (as per your specs), just add 0-9 at the end of the character classes.
^[A-Za-z][A-Za-z0-9_]{3,14}$
try this
This will match any alphanumeric at the beginning and end. In the middle it will accept from one up to twelve alphanumerics including an underscore:
^[a-zA-Z\d]\w{1,12}[a-zA-Z\d]$
It does not match bad.example but matches only example as your regex allows a character from 4 to 15.See here.
http://regex101.com/r/xV4eL5/5
To prevent it you need to match the whole input and not make partial matches.Put a ^ start anchor and $ end anchor.
Use
\A[A-Za-z0-9][\w]{1,12}[A-Za-z0-9]\Z

Regular Expression for re-verification

I am trying to validate verification question and this is the regular expressin I have, I am not what this mean but this expression not allowing spaces
^\S+$
For example if I enter 'Test Me', this expresson says it is not valid.. How do I fix this to allow spaces?
What exactly are you trying to match?
^ matches the beginning of the string
$ matches the end of the string
+ allows multiple occurances of the last expression
\S stands for anything but a whitespace
\s stands for white-spaces
The expression you have will match any string containing only non-white-space characters. If you could express what exactly you're trying to match, I could help you with it.
^\S+$
^^ ^^
|| ||
^ start of string-------------+| ||
\S anything but a whitespace----+ ||
+ one or more of what precedes---+|
$ end of string-------------------+
(visit regular-expressions.info for a larger reference)
Not sure what you want to change, really, since this regular expressions seems to have been written for the sole purpose of not allowing spaces.
^ means "start of the string"
\S is a special keyword in Regex that denotes "non-white space characters"
+ means find the previous one or more times
$ means "end of the string"
So in English, this Regex says: starting at the start of the string, find me ONLY non-white space characters one or more times before the end of the string. This is why it doesn't permit white space.
The reason it does not match is because you are not allowing white space characters in your string with \S
something that might serve you better is:
^[\w\s]+$
\w is equivalent to [A-Za-z0-9_]
\s matches whitespace
keep in mind that this regex will not allow punctuation, if you want that you may be better off using ^.+$

How to use regex for field validation on whole string?

I've been working for many hours trying to do a "simple thing": use a regex to validate a text field.
I need to make sure of:
1- Only use (a-z), (A-Z) and (0-9) values
2- Add a SINGLE wildcard only at the end.
Ex.
Match
MICHE*
Match
JAMES
No match
MICHE**
No match
MIC_HEAL*
I have this regex till now:
[a-zA-Z0-9\s-]+.\z*?
The problem is it still matches when I introduce an invalid character as long as I have a matching sub-string See my REGEX
What can I do to force a match on the whole string? What am I missing?
Thx!
Use ^ (start of line) and $ (end of line) to only match the whole string:
^[a-zA-Z0-9\s-]+.\z*?$
(If you have a multiline input you can also use \A and \z - start and end of string)
On a second look, I don't understand the end of your regex: . (anything) \z * ? (end of string, zero or more times, zero or one time). This regex will match something like:
Ikdflfdf&
Is that correct? If you only want the character *, you should use:
^[a-zA-Z0-9\s-]+\*?$
Also, as Robbie pointed out, you're including spaces and the - in your list of accepted characters. If you only want letters and digits, a shortcut would be using \w (word characters):
^\w+\*$
However, depending on whether the matcher is Unicode-aware or not, \w will also match non-ASCII letters and digits, which may or may not be what you want.
Try this one :
^[a-zA-Z0-9]+\*?$
^ string start
$ string end
* is meta character so it should be escaped like \* to use it as a letter
I think you just need ^ at the begining and $ at the end
^[a-zA-Z0-9\s-]+.\*?$
Also, you don't need the \z
Also, you haven't mentioned that you want to allow spaces and dashes - but you have included them in your allowed character set.

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".