0

So, I got an regular expression :

(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])

That should found all letters and replace it with a blank.

var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])", " ");

But when I got for example :

45a, nomDoc become 45 a, while I juste want 45

Did I write this regex wrong? I'm not very good at it, but I was thinking I was good for this one.

The regex must replace all non-numeric characters, following a numeric character or all non-numeric char before numeric.

45a or a45 must give me 45.

Thank you.

provençal le breton
  • 1,428
  • 4
  • 26
  • 43
  • Could you create a JSFiddle so we can test and help fixing your issue ? – Theox Dec 06 '13 at 09:08
  • 5
    What programming language are you using? – LightningBoltϟ Dec 06 '13 at 09:08
  • 1
    If you replace with *blanks* (single whitespace characters) rather than an empty string, how can you expect the result to be just `45`, without any blank, after replacing the non-numerical characters? – O. R. Mapper Dec 06 '13 at 09:10
  • 8
    If I understand you correctly, you're trying to replace anything non-numeric with one space per character. So why don't you just replace every occurrence of `[^0-9]` with ` `? – n.st Dec 06 '13 at 09:12
  • @Me123 a regex still a regex, whatever the language right? The important part is the regex, not the replace. n.st : forgot : all non-numeric, following numeric . – provençal le breton Dec 06 '13 at 09:23
  • @n.st because my string isn't just 45a but 45a title - some text. So here, I just want to obtain 45 title - some text. – provençal le breton Dec 06 '13 at 09:36
  • @Zaphod Some differ: http://stackoverflow.com/questions/1973343/regular-expressions-same-for-all-languages – LightningBoltϟ Dec 06 '13 at 09:38
  • 1
    @Theox Why would you ask for a JSFiddle in a non-javascript question? – GenericJon Dec 06 '13 at 09:40

5 Answers5

1

What you're doing, is searching for a spot where the string changes from digits to letters or from letters to digits and insert a space there. So yes, 45a becomes 45 a.

If you want to replace all letters with a blank, use

 var nomDoc = Regex.Replace(arr[0], "[A-Za-z]", " ");

But I doubt that this is what you want.
If you want to remove all letters, replace with an empty string instead of a space.

If you want to replace all letters following a digit with a space, use

var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");
SQB
  • 3,926
  • 2
  • 28
  • 49
1

Try this:

var str = "1 oo 23ksls 4910fsj2jd43ld fkkd ^&?&;@";
var nomDoc = str.Replace('/([^0-9]|\n)/g', ' ');

This replaces all the non-number characters(letters, whitespaces and characters) with a space.

LightningBoltϟ
  • 1,383
  • 2
  • 10
  • 28
0

You could try this :

var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");

If you are using Javascript, here's a fiddle :

var Str = "blablabla22445543__-_-_-_-_-_-èèpzofez5zsqef*f-e+ffnfuf'3";

var nomDoc = Str.replace(/[^0-9]/g, "");

$("#result").html(nomDoc); 

http://jsfiddle.net/ZqF6L/

Theox
  • 1,363
  • 9
  • 20
0

It's not quite clear if you want to replace all non-numeric characters with spaces or just remove then completely.

Depending on that, either

var nomDoc = Regex.Replace(arr[0], "[^0-9]", " ");

or

var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");

should do what you want.

n.st
  • 946
  • 12
  • 27
0

hey if you want to remove all your words then use below format method

var demo= Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");