regular expression no characters - regex

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

Related

Match everything before last dot regex

I need a regex that will match everything before a last dot in my string. For example, I have text like this:
if_blk4.if_blk1.if_blk1
I would like to get the if_blk4.if_blk1.
Thanks!
To match everything up to (but not including) the last dot, use a look ahead for a dot:
.*(?=\.)
The greedy quantifier * makes the match include as of the input much as possible, while the look ahead (?=\.) requires the next character in the input to be a dot.
How about
regexp {.*(?=\.[^.]*$)} $text match
i.e. matching any characters that lead up to a (not matched) sequence of dot and zero or more characters that aren't dots, followed by the end of the string.
(The regular expression {.*(?=\.)} is equivalent as regular expression matching doesn't need to be anchored.)
or (faster)
file rootname $text
Documentation:
file,
regexp,
Syntax of Tcl regular expressions

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 can I have two wildcards in this regex expression?

trying to get the following regex: <- bad english from me :(
I'm trying to get the following input text converted as regex...
xx.*.aaa.bbb*
where * are wildcards .. as in .. they represent wildcards to me .. not regex syntax.
Any suggestions, please?
Update - example inputs.
xx.zzzzzzzzz.aaa.bbb = match
xx.eee.aaa.bbbzzzz = match
xx.eee.aaa.bbb.zzzz = match
xx.aaa.bbb = not a match
You misunderstood the concept of * in Regular Expressions.
I think what you are looking for is:
xx\..*\.aaa\.bbb.*
The thing is:
a . is not a real .. It means any character, so if you want to match a . you must escape it: \.
* means that the character that preceeds it will be matched 0 or many times, so how to emulate the wildcard you are looking for? Using .*. It will match any character 0 or many times.
If you want to match exactly the entire string, and not any substring that matches the pattern, you have to include ^ at the begining and $ at the end, so your regex will be:
^xx\..*\.aaa\.bbb.*$
Try this expression:
^xx\.[^\.]+\.aaa\.bbb.*
Assuming that you're saying that * is a wildcard in the 'normal sense', and that your string isn't an attempt at regex, I'd say that xx\..+\.aaa\.bbb.+ is what you're after.
What you refer to as "wildcard -- not regex syntax" is from globbing. It's a pattern matchnig technique that was popularized in the first Unix version in the late 60's. Originally it was a separate program -- called glob -- that produced a result that could be piped to other programs. Now bash, MS-Dos and almost any shell has this feature built-in. In globbing * normally means match any character, any number of times.
The regex syntax is different. The .* idiom in regex is similar to the * in globbing, but not exactly the same. Normally, .* doesn't match line-breaks. You usually have to set the single-line mode (in Ruby called multi line) if you want .* to match any character, any number of times in regex.
* are not wildcards, they mean the preceeding character is repeated 0 or 1 or many times.
And the dot can be any character.
UPDATE:
You can try this
^xx\.[a-z]+\.aaa\.bbb\.?[a-z]*
and you can test it for example here online on rubular
The [a-z] are character groups, within you can define what character is allowed (or not allowed using [^a-z]). so if you are only looking for lowercase letters then you can use [a-z].
The + means it has to there at least once.
The \.? near the end means there can be a dot or not
The ^ at the beginning means to match at the start of the string
A nice tutorial (for Perl, but at least the basics are the same nearly everywhere) is the PerlReTut

Regular expression not matching specific string

My use case is as follows: I would like to find all occurrences of something similar to this /name.action, but where the last part is not .action eg:
name.actoin - should match
name.action - should not match
nameaction - should not match
I have this:
/\w+.\w*
to match two words separated by a dot, but I don't know how to add 'and do not match .action'.
Firstly, you need to escape your . character as that's taken as any character in Regex.
Secondly, you need to add in a Match if suffix is not present group - signified by the (?!) syntax.
You may also want to put a circumflex ^ to signify the start of a new line and change your * (any repetitions) to a + (one or more repititions).
^/\w+\.(?!action)\w+ is the finished Regex.
^\w+\.(?!action)\w*
You need to escape the dot character.
\w+\.(?!action).*
Note the trailing .* Not sure what you want to do after the action text.
See also Regular expression to match string not containing a word?
You'll need to use a zero-width negative lookahead assertion. This will let you look ahead in the string, and match based on the negation of a word.
So the regex you'd need (including the escaped . character) would look something like:
/name\.(?!action)/

Help with Regex patterns

I need some help with regex.
I have a pattern AB.* , this pattern should match for strings
like AB.CD AB.CDX (AB.whatever).and
so on..But it should NOT match
strings like AB,AB.CD.CD ,AB.CD.
AB.CD.CD that is ,if it encounters a
second dot in the string. whats the
regex for this?
I have a pattern AB.** , this pattern should match strings like
AB,AB.CD.CD, AB.CD. AB.CD.CD but NOT
strings like AB.CD ,AB.CDX,
AB.whatever Whats the regex for
this?
Thanks a lot.
Looks like you've got globs not regular expressions. Dot matches any char, and * makes the previous element match any 0+ times.
1) AB\.[^.]*
Escape the first dot so it matches a literal dot, and then match any character other than a dot, any number of times.
2) "^(AB)|(AB\.[^.]*\.[^.]*$"
This matches AB or AB followed by .<stuff>.<stuff>
http://www.regular-expressions.info/ contains lots of useful information for learning about regular expressions.
If your regex engine supports negative lookahead you might try something like:
^AB\.[^.]+$
^AB(?!\.[^.]+$)
(or
^AB\.[^.]*$
^AB(?!\.[^.]*$)
if you want to allow AB. )
I don't find you're question entirely clear; please comment here (or edit your question if you can't add comments) if I'm getting this wrong but what I think you're looking for is:
1) matching strings "AB.AnyTextHereWithoutDots" but not "AB" or "AB.foo." etc
If so a matching regex would be:
"^AB\.[^.]*$"
2) matching "AB" or "AB.something.something" with either none or two or more dots
If so a matching regex would be something like:
"^AB(\..*\..*)?$" or "'^AB\(\..*\..*\)\?" (depending on the nature of your regex engine)
As Douglas suggests matching with globs would likely be easier.
And as spdenne suggests, find a good regex reference.
I tried this in vim. Here is the sample data:
AB.CD
AB.CDX
AB.whatever
AB
AB.CD.CD
AB.CD.
AB.CD.CD
Here is my regexes
This captures all lines starting with AB and then expects a literal dot, and then filters out all lines that has a second dot.
^AB\.[^.]*$
This captures all lines that is just an AB (the part before the pipe) or lines that start with AB that is followed by two literal dots (escaped with a backslash)
^AB$\|^AB\..\..$