Search code examples
regextextmate

Regular expression: Changing part of a line starting with a character


I am trying to write a regular expression that finds lines starting with '@' and finds a particular character and replaces it. Concretely, I want to find lines such as:

@xxxxxx{yyy/zzz

and replace this with

@xxxxxx{yyy_zzz

(xxxxxx, yyy and zzz do not have /)

I can come up with the following that starts from the beginning of a line and finds the first /

^(.*?)/(.*?)

then I can change this with

$1_$2

But this picks up more lines with / in them and I want to focus them on lines that start with @. I would appreciate help. I am doing this within Textmate, to be more specific.


Solution

  • ^(@.*?)\/(.*) will get @xxxxxx{yyy and zzz in the groups 1 and 2 for @xxxxxx{yyy/zzz. Note that I escaped /. You might need to change that depending on the language you're using.

    I used to use the website below for my assignments. It might help you for your next regular expressions.

    WEBSITE: https://regex101.com/