C# Regex.Split is working differently than JavaScript
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
I'm trying to convert this long JS regex to C#.
The JS code below gives 29 items in an array starting from ["","常","","ã«","","最新","ã€Â","最高"...]
var keywords = /( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)/g;
var source = '常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。';
var result = source.split(keywords);
But the C# code below gives a non-splitted single item in string
.
var keywords = @"/( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)/g";
var source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
var result = Regex.Split(source, keywords);
Many questions in Stack Overflow are covering relatively simple expressions only, so I cannot find my mistakes.
What am I missing?
javascript c# regex japanese
add a comment |Â
up vote
10
down vote
favorite
I'm trying to convert this long JS regex to C#.
The JS code below gives 29 items in an array starting from ["","常","","ã«","","最新","ã€Â","最高"...]
var keywords = /( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)/g;
var source = '常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。';
var result = source.split(keywords);
But the C# code below gives a non-splitted single item in string
.
var keywords = @"/( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)/g";
var source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
var result = Regex.Split(source, keywords);
Many questions in Stack Overflow are covering relatively simple expressions only, so I cannot find my mistakes.
What am I missing?
javascript c# regex japanese
add a comment |Â
up vote
10
down vote
favorite
up vote
10
down vote
favorite
I'm trying to convert this long JS regex to C#.
The JS code below gives 29 items in an array starting from ["","常","","ã«","","最新","ã€Â","最高"...]
var keywords = /( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)/g;
var source = '常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。';
var result = source.split(keywords);
But the C# code below gives a non-splitted single item in string
.
var keywords = @"/( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)/g";
var source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
var result = Regex.Split(source, keywords);
Many questions in Stack Overflow are covering relatively simple expressions only, so I cannot find my mistakes.
What am I missing?
javascript c# regex japanese
I'm trying to convert this long JS regex to C#.
The JS code below gives 29 items in an array starting from ["","常","","ã«","","最新","ã€Â","最高"...]
var keywords = /( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)/g;
var source = '常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。';
var result = source.split(keywords);
But the C# code below gives a non-splitted single item in string
.
var keywords = @"/( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)/g";
var source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
var result = Regex.Split(source, keywords);
Many questions in Stack Overflow are covering relatively simple expressions only, so I cannot find my mistakes.
What am I missing?
javascript c# regex japanese
edited Sep 4 at 8:01


Pang
6,682156399
6,682156399
asked Sep 4 at 1:58
Youngjae
13k1170138
13k1170138
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
14
down vote
accepted
Your RegEx is wrong, you should not start and end with '/'
or '/g'
You specify a string in the constructor, not a JavaScript Regex (with '/ /' syntax.). That's a Javascript syntax.
Actually the same applies to JavaScript when you use a string constructor like this:
var regex = new RegExp('//');
// This will match 2 slashes
Thanks, I removed first/
and last/g
then it works.
– Youngjae
Sep 4 at 2:20
add a comment |Â
up vote
0
down vote
Here is a C# example code
string keywords = @"( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)";
string source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
string res = Regex.Split(source, keywords);
string single = "";
foreach ( string str in res )
single += "'" + str + "',";
Console.WriteLine("0", single);
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
14
down vote
accepted
Your RegEx is wrong, you should not start and end with '/'
or '/g'
You specify a string in the constructor, not a JavaScript Regex (with '/ /' syntax.). That's a Javascript syntax.
Actually the same applies to JavaScript when you use a string constructor like this:
var regex = new RegExp('//');
// This will match 2 slashes
Thanks, I removed first/
and last/g
then it works.
– Youngjae
Sep 4 at 2:20
add a comment |Â
up vote
14
down vote
accepted
Your RegEx is wrong, you should not start and end with '/'
or '/g'
You specify a string in the constructor, not a JavaScript Regex (with '/ /' syntax.). That's a Javascript syntax.
Actually the same applies to JavaScript when you use a string constructor like this:
var regex = new RegExp('//');
// This will match 2 slashes
Thanks, I removed first/
and last/g
then it works.
– Youngjae
Sep 4 at 2:20
add a comment |Â
up vote
14
down vote
accepted
up vote
14
down vote
accepted
Your RegEx is wrong, you should not start and end with '/'
or '/g'
You specify a string in the constructor, not a JavaScript Regex (with '/ /' syntax.). That's a Javascript syntax.
Actually the same applies to JavaScript when you use a string constructor like this:
var regex = new RegExp('//');
// This will match 2 slashes
Your RegEx is wrong, you should not start and end with '/'
or '/g'
You specify a string in the constructor, not a JavaScript Regex (with '/ /' syntax.). That's a Javascript syntax.
Actually the same applies to JavaScript when you use a string constructor like this:
var regex = new RegExp('//');
// This will match 2 slashes
edited Sep 5 at 15:46
answered Sep 4 at 2:05
Poul Bak
1,7351519
1,7351519
Thanks, I removed first/
and last/g
then it works.
– Youngjae
Sep 4 at 2:20
add a comment |Â
Thanks, I removed first/
and last/g
then it works.
– Youngjae
Sep 4 at 2:20
Thanks, I removed first
/
and last /g
then it works.– Youngjae
Sep 4 at 2:20
Thanks, I removed first
/
and last /g
then it works.– Youngjae
Sep 4 at 2:20
add a comment |Â
up vote
0
down vote
Here is a C# example code
string keywords = @"( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)";
string source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
string res = Regex.Split(source, keywords);
string single = "";
foreach ( string str in res )
single += "'" + str + "',";
Console.WriteLine("0", single);
add a comment |Â
up vote
0
down vote
Here is a C# example code
string keywords = @"( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)";
string source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
string res = Regex.Split(source, keywords);
string single = "";
foreach ( string str in res )
single += "'" + str + "',";
Console.WriteLine("0", single);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Here is a C# example code
string keywords = @"( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)";
string source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
string res = Regex.Split(source, keywords);
string single = "";
foreach ( string str in res )
single += "'" + str + "',";
Console.WriteLine("0", single);
Here is a C# example code
string keywords = @"( |[a-zA-Z0-9]+.[a-z]2,|[一-龠々〆ヵヶã‚Â]+|[ãÂÂ-んã‚Â]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[ï½Â-zA-Zï¼Â-9]+)";
string source = @"常ã«最新ã€Â最高ã®モãƒÂイル。Androidを開発ã—ãŸåÂ΋˜ãƒÂームã‹ら。";
string res = Regex.Split(source, keywords);
string single = "";
foreach ( string str in res )
single += "'" + str + "',";
Console.WriteLine("0", single);
answered Sep 4 at 2:57
sln
25.4k31536
25.4k31536
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52157845%2fc-sharp-regex-split-is-working-differently-than-javascript%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password