-4

I'm looking for a way to subtract a fixed amount if the number is bigger than specified value - using regex

e.g.

If the number is bigger than 10000 I want to subtract 5000, so it should look like:

175 -> 175
7831 -> 7831
12091 -> 7091

user2016412
  • 59
  • 1
  • 2
  • 5
  • 4
    What language? Also regex isn't the way to do math. – Qtax Jan 28 '13 at 10:14
  • Probably better to use some form of coded non-regex logic. Regex is not for everything... – Alderath Jan 28 '13 at 10:15
  • 2
    You can't do math operations with regular expressions. Your solution is way easier than regexes anyway `if (value > 10000) value =- 5000;` – m0skit0 Jan 28 '13 at 10:17
  • I think C#, I use Visual Web Ripper software and I'm trying to create a content transformation script. – user2016412 Jan 28 '13 at 10:19
  • @m0skit0, you can do some, but it's not very nice. ;-) – Qtax Jan 28 '13 at 10:20
  • @Qtax can you show me an example of math operation with regex? – m0skit0 Jan 28 '13 at 10:33
  • @m0skit0 http://stackoverflow.com/questions/5245087/math-operations-in-regex example there, but works only in perl (perl evaluates the expression, not regex itself). – Destrictor Jan 28 '13 at 10:56
  • 1
    @m0skit0, example: Add 1 to all integers `2 > x >= 0`: `s/\b1\b/2/g; s/\b0\b/1/g;`. In a [unary numeral system](http://en.wikipedia.org/wiki/Unary_numeral_system) you can add one just by using `s/^/1/`. In binary you could use an expression like `s/(?:0|\b(?=1)|(\G1))(?=1*$)/$1?0:1/ge`. I had to use `/e` here, but there could be a workaround using multiple expressions. (Perl syntax.) – Qtax Jan 28 '13 at 11:08
  • @Qtax: cool examples :) Although they don't work for decimal system, which is the point of the question. – m0skit0 Jan 28 '13 at 11:21

1 Answers1

2

Regex is used for pattern matching, replacing text.

You cannot use regex to do mathematical operations.

At max, in C# you can do this:

String s = Regex.Replace(input, @"\b\d{5,}\b", m => (int.Parse(m.Value)-5000).ToString());

So,

44 10000 15000 1 100

would become

44 5000 10000 1 100
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Anirudha
  • 32,393
  • 7
  • 68
  • 89