Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression no characters

Tags:

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

like image 280
Decrypter Avatar asked Oct 03 '11 14:10

Decrypter


People also ask

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

Can a regular expression be empty?

∅, the empty set, is a regular expression. ∅ represent the language with no elements {}.

What is the regular expression for blank?

The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string.


1 Answers

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.

like image 114
Donut Avatar answered Sep 21 '22 07:09

Donut