Treat CNPJ’s list with regular expression

Asked

Viewed 71 times

-1

I have a list of CNPJ’s, I would like to take this list and add simple quotes and separate each of them with comma, doing this with regular expression.

Ex. My list:

32132132132 32132132132 32132132132132 32132132132

How I wish you to stay:

'32132132132','32132132132','321321321323','32132132132132132'

  • 2

    Language is missing, Regexs (Regular Expressions) work differently depending on the Language

  • 1
  • I would do something like: demo, that would work in some of the simplest text editors... Your question would be more to replace in a list that has space as separator, to comma as separator and to put apostrophe delimiting each element. And not treat CNPJ...

1 answer

0


I honestly see no reason to use Regular Expressions in something simple.

In PHP utilize explode and join

$str = '32132132132 32132132132 321321321323 32132132132132';
$exp = explode(' ', $str);
echo "'" . join("';'", $exp) . "'";

See working in repl it.

In Python utilize split and join

str = '32132132132 32132132132 321321321323 32132132132132';
exp = str.split();
print("'" + "';'".join(exp) + "'")

See working in repl it.

In Java utilize split and join

String link = "32132132132 32132132132 321321321323 32132132132132";
String[] partes = link.split(" ");
System.out.println("'" + String.join("';'", partes) + "'");

See working in repl it.

In Javascript utilize split and join

let str = '32132132132 32132132132 321321321323 32132132132132';
let res = "'" + str.split(' ').join("';'") + "'";
console.log(res)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.