Regex for checking the last character

regex match last character
regex to check first and last character
regex last two characters
regex last occurrence of character
regex replace last character
regex get last character
regex first character
regex start and end with same character

How can I check if the last character of a string is ' ' or ','?

I know for checking last character of string we need use $, and I made this regex for checking that the last character is ',':


But I also need to check whether it's a space or not.

It will be very helpful if you give suggestion for making a regex like this.

You can use [] to provide a set of characters:


which means, that you want either , or space character at the end of the string

Regex to match last character, cat I catI catII cat II cat0DOG I cat III dog I dog III bird I. Add Test. Build a suite of tests that your expression should (or should not) match. Create new tests with the � I'm trying to use regex to check that the first and last characters in a string are alpha characters between a-z. I know this matches the first character: /^[a-z]/i But how do I then check for the last character as well? This: /^[a-z][a-z]$/i does not work. And I suspect there should be something in between the two clauses, but I don't know what!

Regexps don't come for free and are often much slower than using a simple method on String.

Therefore I would suggest to not use a regexp in thus case but String#end_with? instead:

string.end_with?(' ', ',')

IMHO it is also easier to read and to understand too.

How do I use regular expression to match the last character of a , expression that might be helpful, which matches if the last character in the string is "." or "!" or "?". >> candidate = 'Is this a sentence?';. >> regexp(candidate, '. C#: Fastest Way To Check Last Character of a String. In this article we’ll benchmark in C#: Fastest Way To Check Last Character of a string. There are plenty of applications and systems out there which require the need to check last character of a string.

You can use a dot . to match any character except a new line. When you use the s flag, the dot can also match a new line.


Regex - Match Start or End of String (Line Anchors), Dollar ($) matches the position right after the last character in the string. 2. Regex patterns to match start of line. Description, Matching Pattern. 1. Line Anchors. In regex, anchors are not used to match characters.Rather they match a position i.e. before, after, or between characters. To match start and end of line, we use following anchors:

regular expression to check the last character is digit [Solved] (Java , what should be the correct regexp to check the last character is digit (0-9)?. Post by: John Jai , Rancher. Mar 19, 2012 22:29:08 (1 like). In regex, we can match any character using period "." character. To match only a given set of characters, we should use character classes. 1. Match any character using regex '.' character will match any character without regard to what character it is. The matched character can be an alphabet, number of any special character.

Anchors: string start ^ and end $, The caret ^ and dollar $ characters have special meaning in a regexp. Similar to this, we can test if the string ends with snow using snow$ :. The tables below are a reference to basic regex. While reading the rest of the site, when in doubt, you can always come back and look here. (It you want a bookmark, here's a direct link to the regex reference tables).

Regex help needed - How to check the second to last character?, Howdy,I need to do a Regex check to see if the second-to-last character of a string is an even or odd number. I know I can jus | 4 replies | IT� so if you wanted to remove the last 2 characters only if they were digits: (.*)\d{2}$ is looking for anything followed by two digits. (.*)[.]\w{2,3}$ is looking for anything followed by a decimal point followed by 2 or 3 word characters at the end of the word. I put both solutions (regex and string functions) in case you wanted to see the

  • Careful. $ is end of line in Ruby, not end of string. You want \z for end of string. Two comments on the answer mention this but it is such a common error with Ruby regexes that it needs to be mentioned more times.
  • i mean to say only space, not white space, updated OP
  • (1) The backslash in your regex, while doing no harm, is meaningless. (2) If you want to check for a space instead of a comma, why not simply writing a space instead of a comma? / \z/
  • I believe you are looking for str[/[, ]]\z/, which will return a space, comma or nil. You could instead use the regex /(?:,| )\z/.
  • @TimBiegeleisen i don't understand your point, can you explain me ?
  • @Vishal Igor fixed his answer, nothing to explain.
  • Use [,\s]\z. $ means "end of line", not "end of string"
  • Igor, can you please update answer as per @Phlip's suggestion ?
  • This fails on a multiline string that has no trailing space or comma, e.g. on "foo,\nbar". Always prefer \A and \z matchers over ^ and $ unless you explicitly want to match the end of line, not the end of string. NB: OP wanted to match characters in the end of the string.
  • For str = "It's a fine day " and n = 10_000_000 I compared n.times { str.end_with?(' ', ',') } with n.times { str.match? /[ ,]/ }. The former took 1.79 seconds, the latter, 1.58 seconds.
  • Sure, the pure string methods are usually faster than the regexp versions.