Regular Expressisons

I need to get an output as “1234560” based on Error message as “The code is not generated” using regex. anybody can help?

Error For Record 2 : 1234559remarks
Error Message :The code is generated.

Error For Record 2 : 1234560numeric
Error Message :The code is not generated.

Can you be more clear

Hi @Manikandasamy

you want only numbers based on regex

Thanks
Ashwin S

For getting only numbers try this regex

\d[0-9]+

Thanks
@Manikandasamy

@Manikandasamy If you want only numbers and if this number is having only 7 digits than you can use this regex

1 Like

I need only numbers, but i should get the numbers based on Error message, if the error message is " The code is not generated" then i should take the digits.

Where you get this error?
means where u will look for this error?

the error message is below to that line.

Error For Record 2 : 1234559remarks
Error Message :The code is generated.

Error For Record 2 : 1234560numeric
Error Message :The code is not generated.

So before applying Regex use if condition
If Varible.Contains( The code is not generated)
Then
Apply Regex for Digits
\d[0-9]+

The text file will be having many Error messages, i should take digits only if the error message is (The code is not generated), is there a way? can u explain in detail

Say Your Text Having i.e
variable=
“Error For Record 2 : 1234559remarks
Error Message :The code is generated.”

Then as i have said earlier
If Varible.Contains( “The code is not generated” )
Then
Apply Regex for Digits
\d[0-9]+
Else
leave it

It will check for that specific error if present then only it will check for digits

@Manikandasamy I guess if Error Message :The code is not generated. than Error For Record will have numeric keyword so in this case you can use this regex

1 Like

Hi
if this is stored in a string variable named
str_input = “Error For Record 2 : 1234559remarks
Error Message :The code is generated.”

then the OUTPUT will be with a assign activity like this
str_input = String.Join(“”, str_input.Split(Environment.NewLine.ToArray()))

followed by that another assign activity like this
str_output = IF(str_input.ToString.Contains(“The code is not generated”),System.Text.RegularExpressions.Regex.Match(str_input.ToString,“\s[0-9]{2,}”).ToString.Trim,String.Empty)

so that the output will be like this “1234559”

Cheers @Manikandasamy

When code is not generated.
The number is followed by numeric or it changes everytime
i.e. 1234560numeric
If it is same then use this Regex

\d+(?=numeric)

Thanks
@Manikandasamy

will i get an output even if my string has multiple error messages? simply saying, with this code can i get more than one output?

1 Like

numeric is not a constant string, so we cant take that

The above suggested would give only one output buddy
and if we want for multiple occurence then use MATCHES activity where in the input string mention the variable str_input after this assign

then mention the Expression with a value as “(?<=Record).*(?=The code is generated)”

and get the output with a variable named out_matches

–now use a FOR EACH activity and pass the above variable out_matches as input and change the type argument as System.Text.RegularExpressions.Match and inside the loop use a WRITE LINE ACTIVITY like this
System.Text.RegularExpressions.Regex.Match(item.ToString,“\s[0-9]{2,}”).ToString.Trim

where item is the variable from for each loop
this will display all such matching values

Cheers @Manikandasamy

Hi @Manikandasamy

then just pass \d{7}+

Thanks
Ashwin S

Use this Code and assign it to a string will give you the desired output:

System.Text.RegularExpressions.Regex.Match(tt,“(?<=:.)(\d+)(?=.)(.*\n)(?=Error Message :The code is not generated)”,System.Text.RegularExpressions.RegexOptions.Compiled).Groups(1).Value

Thanks
@Manikandasamy

The regex is working fine, can you pls help me to get an output, when i try this its returning a null value in uipath