Search code examples
regexdreamweaver

REGEX exclude if there's a dash before number and if inside other string


I've been trying to write a regular expression that does exactly as stated in the title. I need to find all occurences of xA, or xkA, or xmA (and variation eg. x k A or xk A) to format those amperes later on: x A, x kA, x mA.

Unfortunately with the regex I have it selects number+A whenever it finds it:

F-2A WACEGF2AOKR 10m A AC

F-3A WACEGF3AOKR 10k A AC

C-7A WACEGC7AOKR 20m A

My regex:

([0-9]+)([m|k])? ?A

I found some solution online:

[^- a-zA-Z]([0-9]+)([µ|µ|m|M|k|G|]) ?A

but it fails while run in Dreamweaver (and I HAVE TO use it in DW, it's part of bigger command) - it "eats" part of the string in the backreference, making eg. "10" out of "1000"

Edited:

\b([0-9]+)([µmMkG]|µ)\s*A 

(?<![0-9])([0-9]+)([µmMkG]|&micro;)\s*A

The first one selects as follows: enter image description here

The second one doesn't select anything :(

Note I am trying not to remove the char before the number, i.e. > or / that are removed if I use the following:

enter image description here


Solution

  • You may use the following regex:

    (^|[^\w-])\b([0-9]+)([µmMkG]|&micro;)? ?A
    

    When replacing, mind you may access the text captured with capturing groups using $ + a digit identifying the group. E.g. (^|[^\w-]) text is access via $1, ([0-9]+) text is accessed via $2, ([µmMkG]|&micro;) can be reached via $3.

    See the regex demo.

    Details

    • (^|[^\w-]) - Group 1: start of string or any char other than letters, digits, underscore and -
    • \b - a word boundary
    • ([0-9]+) - Group 2: one or more digits
    • ([µmMkG]|&micro;)? - Group 3 (optional): µ, m, M, k, G or &micro;
    • ?A- an optional space and thenA`.