cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
New to using JMP? Hit the ground running with the Early User Edition of Discovery Summit. Register now, free of charge.
Register for our Discovery Summit 2024 conference, Oct. 21-24, where you’ll learn, connect, and be inspired.
%3CLINGO-SUB%20id%3D%22lingo-sub-518525%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3EDividir%20una%20cadena%20para%20extraer%20n%C3%BAmeros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-518525%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CP%3EHola%20%2C%3C%2FP%3E%3CP%3ETengo%20una%20cadena%20que%20es%20de%20caracteres%20y%20quiero%20extraer%20una%20secci%C3%B3n%20de%204%20n%C3%BAmeros.%20La%20posici%C3%B3n%20de%20los%20n%C3%BAmeros%20puede%20cambiar.%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EUn%20ejemplo%20de%20la%20cadena%20puede%20ser%3C%2FP%3E%3CP%3Evalor%20%3D%20%22Hola%200123%20ejemplo%22%3B%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3ELo%20intent%C3%A9%20pero%20solo%20funciona%20si%20los%20n%C3%BAmeros%20est%C3%A1n%20en%20la%20posici%C3%B3n%20correcta.%3C%2FP%3E%3CP%3Evalor%20%3D%20%22Hola%200123%20ejemplo%22%3B%3C%2FP%3E%3CP%3Eextraer%3Dsubstr(valor%2C7%2C%204)%3B%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3EComo%20la%20posici%C3%B3n%20del%20n%C3%BAmero%20cambiar%C3%A1%2C%20%C2%BFc%C3%B3mo%20puedo%20extraer%20esto%3F%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-518525%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CLINGO-LABEL%3EModelado%20estad%C3%ADstico%20avanzado%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-518567%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Dividir%20una%20cadena%20para%20extraer%20n%C3%BAmeros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-518567%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3Eest%C3%A1%20bien%2C%20gracias%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F15446%22%20target%3D%22_blank%22%3E%20%40Aurora_TiffanyD%3C%2FA%3E%20y%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F14366%22%20target%3D%22_blank%22%3E%20%40jthi%3C%2FA%3E%20!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-518566%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Dividir%20una%20cadena%20para%20extraer%20n%C3%BAmeros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-518566%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EEsto%20es%20probablemente%20mejor%20porque%20no%20tienes%20que%20agarrar%20el%20primer%20partido.%C2%A1Gracias!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-518565%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Dividir%20una%20cadena%20para%20extraer%20n%C3%BAmeros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-518565%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EOtra%20opci%C3%B3n%20es%20usar%20Regex%20en%20lugar%20de%20Regex%20Match%20(tambi%C3%A9n%20hay%20m%C3%A1s%20opciones%20sobre%20c%C3%B3mo%20se%20podr%C3%ADa%20hacer%20esto%2C%20pero%20creo%20que%20Regex%20y%20Regex%20Match%20son%20las%20m%C3%A1s%20f%C3%A1ciles)%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(1)%3B%0A%0Astring%20%3D%20%22Hello%200123%20example%22%3B%0Afour_num%20%3D%20Regex(string%2C%20%22%5Cd%7B4%7D%22)%3B%26nbsp%3B%2F%2F0123%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EVer%3CA%20href%3D%22https%3A%2F%2Fregex101.com%2Fr%2F26hBdb%2F1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%20https%3A%2F%2Fregex101.com%2Fr%2F26hBdb%2F1%3C%2FA%3E%20para%20saber%20c%C3%B3mo%20funciona%20la%20expresi%C3%B3n%20regular%20si%20no%20est%C3%A1%20familiarizado%20con%20ella.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-518564%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Dividir%20una%20cadena%20para%20extraer%20n%C3%BAmeros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-518564%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EEsto%20funcion%C3%B3%20a%20las%20mil%20maravillas.%20%C2%A1Muchas%20gracias!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-518561%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Dividir%20una%20cadena%20para%20extraer%20n%C3%BAmeros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-518561%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EPodr%C3%ADas%20intentar%20algo%20como%20esto%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(%201%20)%3B%0A%0Astring%20%3D%20%22Hello%200123%20example%22%3B%0Amatches%20%3D%20Regex%20Match(%0A%20string%2C%0A%20%22%5B0-9%5D%7B4%7D%22%2C%0A)%3B%0A%2F%2F%20ASSUMES%20that%20you%20are%20interested%20only%20in%20the%20first%204-digit%20number%2C%20if%20there%20is%20more%20than%20one%0AextractedNumber%20%3D%20matches%5B1%5D%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EEsto%20devuelve%20%220123%22.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETenga%20en%20cuenta%20que%20a%C3%BAn%20devuelve%20%220123%22%20si%20su%20cadena%20de%20entrada%20es%20%22Ejemplo%20de%20Hola%2001234%22.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
B1234
Level III

Splitting a string to extract numbers

Hi , 

I have a string that is character and I want to extract a 4 number section. The position of the numbers may change

 

An example of the string may be

value ="Hello 0123 example";

 

I have tried this but only works if the numbers are in the correct position. 

value ="Hello 0123 example";

extract=substr(value,7, 4);

 

As the position of the number will change how can I extract this?

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: Splitting a string to extract numbers

You could try something like this:

Names Default To Here( 1 );

string = "Hello 0123 example";
matches = Regex Match(
	string,
	"[0-9]{4}",
);
// ASSUMES that you are interested only in the first 4-digit number, if there is more than one
extractedNumber = matches[1];

This returns "0123".

 

Please note that it still returns "0123" if your input string is "Hello 01234 example".

View solution in original post

jthi
Super User

Re: Splitting a string to extract numbers

One other option is to use Regex instead of Regex Match (there are also more options how this could be done, but I think Regex and Regex Match are the easiest ones)

Names Default To Here(1);

string = "Hello 0123 example";
four_num = Regex(string, "\d{4}"); //0123

See https://regex101.com/r/26hBdb/1 for how regex this works if you are not familiar with it.

-Jarmo

View solution in original post

5 REPLIES 5

Re: Splitting a string to extract numbers

You could try something like this:

Names Default To Here( 1 );

string = "Hello 0123 example";
matches = Regex Match(
	string,
	"[0-9]{4}",
);
// ASSUMES that you are interested only in the first 4-digit number, if there is more than one
extractedNumber = matches[1];

This returns "0123".

 

Please note that it still returns "0123" if your input string is "Hello 01234 example".

B1234
Level III

Re: Splitting a string to extract numbers

This worked like a charm. Thank you so much!

jthi
Super User

Re: Splitting a string to extract numbers

One other option is to use Regex instead of Regex Match (there are also more options how this could be done, but I think Regex and Regex Match are the easiest ones)

Names Default To Here(1);

string = "Hello 0123 example";
four_num = Regex(string, "\d{4}"); //0123

See https://regex101.com/r/26hBdb/1 for how regex this works if you are not familiar with it.

-Jarmo

Re: Splitting a string to extract numbers

This is probably better because you don't have to grab the first match.  Thanks!

B1234
Level III

Re: Splitting a string to extract numbers

ok thank you @Aurora_TiffanyD and @jthi !