Home > Enterprise >  Regex to remove the character immediately after number
Regex to remove the character immediately after number

Time:02-15

I want to remove a substring and number from the string. For example:

  1. AB1 Line 01B
  2. CD12 Line 21
  3. AE2 Line 12a

should return

  1. AB
  2. CD
  3. AE respectively

Currently, I'm using 'Line|[0-9]' regex, which can match everything except the character immediately after the string ('B' in 01B). What changes should I make? Your help is appreciated.

CodePudding user response:

You can use

regexp_replace(column_name, 'Line|[0-9] [a-zA-Z]*', '')

If you wish to also remove whitespace on both ends, you may match whitespaces with [[:space:]]* (or \s*, where \ might need doubling):

regexp_replace(column_name, '[[:space:]]*(Line|[0-9] [a-zA-Z]*)[[:space:]]*', '')
regexp_replace(column_name, '\\s*(Line|[0-9] [a-zA-Z]*)\\s*', '')

Details:

  • \s* - zero or more whitespaces
  • ( - start of a group:
    • Line - a fixed string
    • | - or
    • [0-9] - one or more digits
    • [a-zA-Z]* - zero or more letters
  • ) - end of the group
  • \s* - zero or more whitespaces

CodePudding user response:

var a = "AB1 Line 01B"
var b = a.replace(/\d.*/g, "")
console.log(b)
  • Related