+ Start a Discussion
anurajanuraj 

reg expression

Hi

Please help me i want to get the number value in the bracket () 

456test(tata)(123)

i want 123 from the example 

the number in the bracket changes record by record so i want the number from the bracket 

thanks 

Anuraj

 

 

Starz26Starz26

not goo dwith regex but if the format stays the same you could:

 

**Get the value of the field you are working with and assign it to a String called theString**

 

Sting[] sVal = New String[]();

 

sVal = theString.split('(');

 

sVal[2].replace(')','');

 

then sVal[2] should equal 123

 

I am sure there are better ways but this should work until you get regex figured out.......which reminds me, I need to do some reading...:)

anurajanuraj

i am getting this expextion 

System.StringException: Invalid regex: Unclosed group near index 1 ( ^

 

my code


 String selectvalue = '456TATA(Test)(123)';

 list<String> sVal = new list<string>();
 sVal = selectvalue.split('(');
 sVal[2].replace(')','');

 

 

thanks

Anuraj

Starz26Starz26

Sorry, forgot to escape the (

 

String theString = '456TATA(Test)(123)';
String[] sVal = New String[]{};
sVal = theString.split('\\(');
String finalString = sVal[2].subString(0,sVal[2].length()-1);

 

anurajanuraj

Hi

Thanks for your replay I have done it brfore but i wont the number value in the bracket even if it changes to 

456TATA(Test)(ncnc)(123)

 

the value changes every time. some time it would be 456TATA(Test)(ncnc)(123) or it may change to 

456TATA(123) or 456(123)(Test) etc

 

So i am using  reg expression so that i can get number only

 

Thanks

Anuraj

Starz26Starz26

Again, not familiar with Regex but I do believe if the format constantly changes (i.e. the number is the 2nd pattern, or the 3rd pattern at times, etc it will be difficult. It can be done as long as the value in the ( ) that you want is ALWAYS a number, and the value in the other ( ) is ALWAYS alpha.

 

You basically want to pattern match (?# any number) and return the numbers. I know that someone here is good with regex, I will do some reading int he meantime

Starz26Starz26

I believe this will return the number between the ( and )

 

^?(\d+)

 

incorporating it into code, still need to read more. It does work in a regex builder though.