views:

62

answers:

4

i have regex code:

<script type="text/javascript">
     var str = "kw-xr55und";
     var patt1 = /[T|EE|EJU].*D/i;
     document.write(str.match(patt1));
</script>

it can read:

str= "KD-R55UND" -> as UND

but if i type:

 str= "kw-tc800h2und ->  result tc-800h2und.  //it makes script read T in front of 800
 i want the result as UND 

how to make the code just check at character behind the 800?

EDIT

After this code it can work:

<script type="text/javascript">
var str = "kw-tc800h2und";
var patt1 = /[EJTUG|]\D*D/i;
document.write(str.match(patt1));
</script>

but show next problem, i can show the result if:

str= "kw-tc800un2d"

i want result like -> UN2D
+1  A: 

For PCRE, try this:

/(?<=\d)\D*/

It uses a lookbehind to find a set of non-digit characters that comes immediately after a digit.

For Javascript, try this:

/\D+$/

It will match any characters that are not digits from the end of the text backwards.

Amber
+1 good answer, and thanks for the pointers.
JoshD
@Amber: how to combine with my 1st code? because i need to filtering character which is consist of T,E,EE,U,J,U in each string.
klox
@klox `/[TEUJ]\D*$/`
Darth Android
@Dart: look at Alan answer. it close the answer but show another problem.
klox
@klox Updated my answer, try that one.
Darth Android
A: 

Try /[TEUJ]\D*\d*\D*D$/i if it can have 1 digit in it, but not 2. Getting any more specific would require additional information, such as the maximum length of the string, or what exactly differs between parsing tc800h2und and h2und.

Darth Android
@Darth: have you try it at w3school it makes result as null
klox
@klox fixed, I forgot a `*`.
Darth Android
@Darth: you are missed the KW. my str is KW-TC800H2UND.
klox
@klox I can parse off the `KW-` easily enough, but both `TC800H2UND` and `H2UND` start with the specified characters, end with a `D`, and contain digits. The only other differentiating information is the number of digits they contain, so if you lax the rules any more then we're going to need more examples or a clarified set of rules that you want matched instead of examples.
Darth Android
A: 

Depending on what flavor of regex you're using, you can use a look behind assertion. It will essentially say 'match this if it's right after a number'.

In python it's like this:

(?<=\d)\D*

Oh, also, regex is case sensitive unless you set it not to be.

JoshD
`(?<=\d).*` will fail if there is more than one digit in the number.
Amber
@Amber: right. Thanks for pointing that out.
JoshD
i have try all answer at "Tryit Editor in W3school" but not show the result
klox
The flavor is JavaScript, so lookbehinds are out.
Alan Moore
+1  A: 

Try this:

 var patt1 = /(T|EE|EJU)\D*$/i;

It will match a sequence of non-digit characters starting with T, EE or EJU, and finishing at the end of the string. If the string has to end with D as in your examples, you can add that in:

 var patt1 = /(T|EE|EJU)\D*D$/i;

If you want to match it anywhere, not just at the end of the string, try this:

 var patt1 = /(T|EE|EJU)\D*D/i;

EDIT: Oops! No, of course that doesn't work. I tried to guess what you meant by [T|EE|EJU], because it's a character class that matches one of the characters E, J, T, U or | (equivalent to [EJTU|]), and I was sure that couldn't be what you meant. But what the heck, try this:

 var patt1 = /[EJTU|]\D*D/i;

I still don't understand what you're trying to do, but sometimes trial and error is the only way to move ahead. At least I tested it this time! :P

EDIT: Okay, so the match can contain digits, it just can't start with one. Try this:

var patt1 = /[EJTU|]\w*D/i;
Alan Moore
it makes show result as null
klox
Okay, see my edit.
Alan Moore
another problem show after i have typed "UN2D" it can't show the result
klox
@Alan he said that `tc800h2und` is incorrect, and that the match should return `h2und`, so it seems the requirement of simply containing digits is too lax.
Darth Android
@Alan:i understand both of code but it for different condition. i want we can use one code for all condition.
klox
@Darth: You're right. This would be a lot easier if we could anchor the match to the end of the string: `/[EJTU|]\w*D$/i;`. As it is, I'm not sure it's even *possible*.
Alan Moore