0

Im trying to replace this @Model.<A Word> to $Model.<A Word>$

so, @Model.Test would become $Model.Test$

While searching SO, i found this thread

Regex Until But Not Including

Which the answer worked.. until I had to replace one in this sentence

Élément @Model.Title a besoin d'être\napprouvé

Modifying the answer slightly to : @Model\.((?:(?![ ]).)*)

Results with

Élément $Model.Title a$ besoin d'être\napprouvé

I want the $ to be just after Title -> $Model.Title$

Any help would be appreciated - I'm quite new to regex.

Community
  • 1
  • 1
Alan Ciantar
  • 278
  • 1
  • 3
  • 14

4 Answers4

1

In this case, you definitely don't need look-arounds. Since you are just going to match words after @Model., you can just replace the following regex:

"@(Model[.]\w+)"

with:

"$$$1$$"
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

You can use this pattern:

@Model\.(\w+)

This will match @Model. followed by one or more 'word characters'. For example:

var output = Regex.Replace(
    "Élément @Model.Title a besoin d'être\napprouvé", 
    @"@Model\.(\w+)", 
    "$$Model.$1$$"); // Élément $Model.Title$ a besoin d'être\napprouvé
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
1

Why not just use @Model\.(\S+) with a replacement string $Model.$1$

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • I ended up with p.s.w.g's answer but i tried this anyway (just for reference). Élément @Model.Title a besoin d'être\napprouvé still -> still ends up with $Model.Title a$ – Alan Ciantar Aug 14 '13 at 14:11
  • 1
    @AlanCiantar Are you sure, i just have re-tested that and it was correct. – Ibrahim Najjar Aug 14 '13 at 14:14
  • http://i.imgur.com/swMjwN3.png -- I'm using this site (http://myregexp.com/signedJar.html) -> maybe its old? – Alan Ciantar Aug 14 '13 at 14:17
  • @AlanCiantar Perhaps it is old, nevertheless your problem is solved so it is OK. – Ibrahim Najjar Aug 14 '13 at 14:19
  • @AlanCiantar The difference between this answer and mine (and Rohit's) is that this will match any non-whitespace characters while mine (and Rohit's) will match any word characters. E.g. this will replace `@Model.Title-Text` with `$Model.Title-Text$` while mine will replace `@Model.Title-Text` with `$Model.Title$-Text`. Both are good approaches, depending on exactly what you need. – p.s.w.g Aug 14 '13 at 14:21
  • 1
    @p.s.w.g + Rohit Thank you both :) Apparently Rohit's answer works on other online regex testers O.o - again, ty. – Alan Ciantar Aug 14 '13 at 14:24
0

Try this (Perl/JavaScript regex format):

s/@(Model\.[^ ]+)/\$$1\$/g

Update:

And of course our friend sed:

sed 's/@\(Model\.[^ ]\+\)/$\1$/g'

Update 2:

Removed spaces.

Craig
  • 4,268
  • 4
  • 36
  • 53
  • 1
    The question is tagged C#. Why would you post Perl? Also, this won't work if the word being matched is not immediately followed by a space. e.g. `This is my @Model.Title!`. – p.s.w.g Aug 14 '13 at 14:08
  • Fair comment. But the question seems to be more related to RegExes than it does C# specifics, so I felt providing something to an unanswered (at the time) question, is better than leaving it unanswered. – Craig Aug 14 '13 at 14:19