Regex for Checking the Last Character

Regex validate with last character

Lets analyse your regex:

a - match an "a"

b* - match any number of b's

d - match a "d"

Because * matches any number of b's.

$ matches end of line, so

ab*d$ 

should match end of line (to make sure nothing follows)

Then again \s will match any whitespace so another option is

ab*d\s 

Regex for checking the last character

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

[,\s]\z

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

javascript regex to check if first and last character are similar?

You can use regex with capturing group and its backreference to assert both starting and ending characters are same by capturing the first caharacter. To test the regex match use RegExp#test method.

var regex = /^(.).*\1$/;
console.log( regex.test('abcdsa'))console.log( regex.test('abcdsaasaw'))

Regex to find last character of string

You were almost there - except for two minor mistakes.

string pattern = @"^[a-zA-Z]{2}[0-9]{3}.+[BPJ]$"

That should to the trick. Explanation:

  1. ^[a-zA-Z]{2} - start of the string, followed by two letters
  2. [0-9]{3} - three numeric characters at positions 2,3,4 (not two)
  3. .+ - an arbitrary amount of characters, but at least one - you did not specify any limitations for this part. possibly you want to use [a-zA-Z0-9]+ instead
  4. [BPJ]$ - uppercase letter B, P or J followed by end of the string. in your version, AB000A1| would have matched, too.

The length requirement is implicitly enforced because of the sizes of the parts: 2 + 3 + 1 or more + 1 = 7 or more.

Edit

I misread the "greater than 7" as "at least 7". In that case the pattern must be @"^[a-zA-Z]{2}[0-9]{3}.{2,}[BPJ]$", as Yong Shun pointed out.

Regex to match last character in string

Try this one:

^[a-z|A-Z|0-9]+[^I]\s?I{1}$

I think this is a more accurate solution.

Try demo

Regex expression to check if first and last char of set are different

You can use a capture group for the second char, and only match the last char if it is not the same as capture group 1.

^[ab]([ab])[ab]*(?!\1)[ab]$
  • ^ Start of string
  • [ab] Match a or b (Note that you can omit | as it means a pipe char in the character class
  • ([ab]) Capture group 1, match either a or b
  • [ab]* Optionally match a or b
  • (?!\1) Negative lookahead, assert not the same value as captured in group 1 using the backreference \1
  • [ab]$ match either a or b at the end of the string

Regex demo

Another option is immediately do the assertion after the capture group

^[ab]([ab])(?![ab]*\1$)[ab]*$

Regex demo

Or if supported, as negative lookbehind might also work. This page shows the compatibility for Javascript and lookbehinds

^[ab]([ab])[ab]*[ab]$(?<!\1)

Regex demo

Using Regex, how to check if second to last character is odd

I'd go with /(?<=[13579]{1})[05]|^[05]$/.

This utilises two conditionals. One that checks for the presence of an odd character in the second-to-last position when there's at least two characters in the string, and one that checks for a single character string.

Breaking this down:

  • (?<=[13579]{1}) - does a positive lookbehind on exactly one odd character
  • [05] - match a 0 or a 5 directly following the lookbehind
  • | - denotes an OR
  • ^ denotes the start of the string
  • [05] - match a 0 or a 5
  • $ - the end of the string

This can be seen in the following:

var re = /(?<=[13579]{1})[05]|^[05]$/;console.log(re.test('12345')); // 12345 should return `false`console.log(re.test('12335')); // 12335 should return `true`console.log(re.test('1')); // 1 should return `false`console.log(re.test('5')); // 5 should return `true`

Get last character in string using JavaScript RegExp

// RegExp wayfunction getLastChar1(str){ var r = (/.$/).exec(str); if(r){  return r[0] } return str;}
// last string wayfunction getLastChar2(str){ if(str.length){ return str[str.length-1] } return str;}

console.log(getLastChar1("Hello Word!"))console.log(getLastChar2("Hello Word!"))

How to use regex to tell if first and last character of a string match?

if and only if you really want to use regex (for learning purpose):

import re
string = 'aba'
string2 = 'no match'
pattern = re.compile(r'^(.).*\1$')

if re.match(pattern, string):
print('ok')
else:
print('nok')
if re.match(pattern, string2):
print('ok')
else:
print('nok')

output:

ok
nok

Explanations:

^(.).*\1$
  • ^ start of line anchor
  • (.) match the first character of the line and store it in a group
  • .* match any characters any time
  • \1 backreference to the first group, in this case the first character to impose that the first char and the last one are equal
  • $ end of line anchor

Demo: https://regex101.com/r/DaOPEl/1/

Otherwise the best approach is to simply use the comparison string[0] == string[-1]

string = 'aba'
if string[0] == string[-1]:
print 'same'

output:

same


Related Topics



Leave a reply



Submit