1

Background

I have just begun working with RegEx (last night).

I began matching a single [singleletter]: with the expression below:

Expression: \s+([a-z]:+)

Original string: foo u:james h c:user p:product

Output: ["foo ", "u:", "james h ", "c:", "user ", "p:", "product"]

Problem

I'm trying to modify the RegEx to capture [fullword]: instead of [sigleletter]:. Both expressions below work as desired on regexr.com, but do not work in C#. What am I doing wrong?

Option 1: [a-zA-Z]*([a-zA-Z]:+)

Option 2: \w*([a-zA-Z]:+)

Test string: foo user:james h cust:user prod:product

Desired Output: ["foo", "user:", "james h ", "cust:", "user ", "prod:", "product"]

Fullword definition: case-insensitive a-z (plus the colon)

C# that doesn't work

var foo1 = Regex.Split("cust:test", "[a-zA-Z]*([a-zA-Z]:+)");
var foo2 = Regex.Split("cust:test", "\w*([a-zA-Z]:+)");

Lastly, the first expression that currently works with `[singleletter]:' returns and empty match at the beginning for every string tested, but only in C#. Again, I feel like I'm missing something...

6
  • Regex.Split("foo u:james h c:user p:product", @"\s+"); Commented Sep 9, 2017 at 12:41
  • What is considered to be a fullword? Commented Sep 9, 2017 at 12:43
  • @L.B, I've updated my question to better describe desired output. Again, this works on regexr.com. Commented Sep 9, 2017 at 12:45
  • Put + after character class. \b[a-zA-Z]+: Commented Sep 9, 2017 at 12:45
  • @revo, updated question to define full word Commented Sep 9, 2017 at 12:47

1 Answer 1

1

Different engines work differently. You should try word boundary meta-character in addition to a little bit modification:

\b([a-zA-Z]+:)
Sign up to request clarification or add additional context in comments.

1 Comment

That does it. How frustrating. I'm not sure I quite understand the difference between \w and \b. Time for some more reading. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.