provençal le breton
provençal le breton

Reputation: 1438

Regular expression doesn't match like I want

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.

Upvotes: 0

Views: 113

Answers (5)

Hozefa Laxmidhar
Hozefa Laxmidhar

Reputation: 102

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]+", " ");

Upvotes: 0

LightningBoltϟ
LightningBoltϟ

Reputation: 1373

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.

Upvotes: 1

n.st
n.st

Reputation: 946

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.

Upvotes: 0

SQB
SQB

Reputation: 4078

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]+", " ");

Upvotes: 1

Theox
Theox

Reputation: 1363

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/

Upvotes: 0

Related Questions