ozsu
ozsu

Reputation: 133

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.

Upvotes: 3

Views: 915

Answers (2)

Fatih Aktaş
Fatih Aktaş

Reputation: 1564

^(@.*?)\/(.*) 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/

Upvotes: 3

Poul Bak
Poul Bak

Reputation: 10930

Not being an Expert in Textmate, I hope this will Work:

(?<=@.*?)\/

It Works by looking behind for a '@' sign followed by any number of any sign and finally the slash. The match will consist only of the slash, easy to replace the match with the underscore.

Upvotes: 1

Related Questions