1

I have a list that looks like this:

35yrs
74 yrs
40yrs
24yrs
36 yrs

I want to use regular expressions (in Textmate) to make the list look like this:

35 yrs
74 yrs
40 yrs
24 yrs
36 yrs

I have the search correct:

\d+[y][r][s]

What should the replace look like?

3
  • I don't know the program, but following your search information, would the replace be nearly the same but with a [ ] following the + sign? Blank space between brackets.
    – fred_dot_u
    Commented Apr 4, 2020 at 20:36
  • I'm using textmate which says you should use numbers (e.g., $1) in the replace field. I just don't know what to put as those numbers. Commented Apr 4, 2020 at 20:38
  • Try to put search as (\d+)(yrs), and replace string as $1 $2
    – Philippe
    Commented Apr 4, 2020 at 20:40

2 Answers 2

2

AFAIK, Textmate supports advanced regular expression. Then you can use the following:

  • Find: (?<=\d)(?=yrs)
  • Replace: <-- a space

Explanation:

(?<=\d)    # positive lookbehind, make sure we have a digit before the current position
(?=yrs)    # positive lookahead, make sure we have literally "yrs" after the current position
1
  • This should be marked as the correct answer.
    – leanne
    Commented Feb 10, 2022 at 17:14
1

You just need to use capture groups. Here's the regular expression to use:

(\d+)(yrs)

and in the replace field put this

\1 \2

\1 represents the first group in () and \2 represents the second ()

1
  • This looks like it should work (the regex looks correct), but doesn't - at least as of TextMate version 2.0.23. It replaces the items without spaces between the digits and the yrs with "\1 \2". Weirdness. (The first two lines come out like \1 \2 and 74 yrs, for example.)
    – leanne
    Commented Feb 10, 2022 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .