Category: Regular Expression

Regex to validate alpahanumeric or a set of special characters

October 15, 2013 at 10:58 pmCategory:Regular Expression

I have a requirement to ensure that the string has only alphanumeric or a set of characters like + ( ) , ‘ . – =
I tried like

Bt it is not working, Can anyone assist me please

Thanks in advance

It looks like it will not match a whole string containing a mix of alphanumerics & symbols because of the OR in the middle.

e.g. it wont match abcABC()+, yet will match abcABC & will match ()+


Hope this helps!

How to match an apostrophe (') unless it is escaped (\')?

October 6, 2013 at 10:58 pmCategory:Regular Expression

Is it possible to construct a regular expression for this? If so, I’d appreciate if someone shows how.

Use this regular expression:

It means match all apostrophe chars not preceeded by a backslash (the backslash is escaped itself because it is a special char for regexps)

Splitting a string while keeping the delimiters except escaped ones (regex)

October 5, 2013 at 10:58 pmCategory:Regular Expression

If I have a String which is delimited by a character, let’s say this:

and I want to keep the delimiters, I can use look-behind & look-ahead to keep the delimiters themselves, like:

which results in

  • a
  • -
  • b
  • -
  • c

Now, if one of the delimiters is escaped, like this:

And I want to honor the escape, I figured out to use a regex like this:


Now, this works & results in:

  • a
  • -
  • b\-c

(The backslash I’d after remove with string.replace("\\", "");, I haven’t found a way to include that in the regex)

My Problem is one of understanding.
The way I understood it, the regex would be, in words,

split ((if '-' is before (unless ('\-' is before))) or (if '-' is after (unless ('\-' is before))))

Why shouldn’t the last part be “unless \ is before”? If ‘-‘ is after, that means we’re between ‘\’ & ‘-‘, so only \ should be before, not \\-, yet it doesn’t work if I alter the regex to reflect that like this:

Result: a, -, b\, -c

What is the reason for this? Where is my error in reasoning?

Why shouldn’t the last part be “unless \ is before”?


cursor is after - so "unless \ is before" will always be false since we always have - before current position.

Maybe easier regex would be


  • (?<=(?<!\\\\)-) will check if we are after - that has no \ before.
  • (?=(?<!\\\\)-)will check if we are before - that has no \ before.

Regex before a colon

October 1, 2013 at 10:58 pmCategory:Regular Expression

I have this string:

I want to grab just the X12 part.

I tried:

However, this only gets WALES.

I moreover tried:

But this gets the whole string before & including the colon.

Where am I going wrong?

If you wan’t to find word (\\w+) between &[ & : then try maybe