1

Regex to find a string in C# which starts with a space and ends with a space and have one or more commas in between.

example MELVILLE 203 31-MAR-11 15 DENNY WAY, ALFRED COVE, 6154 WA $19,040 34 POOL

Here I want to get the DENNY WAY, ALFRED COVE, 6154 WA as output

3
  • Thats an incredibly vague question. Either ask it properly or do some research into regex as it seems like there is a simple solution. How about an example of strings that would or wouldn't match? Commented May 3, 2011 at 9:56
  • 1
    According to your recently added example, your specifications are way off. How are the spaces before DENNY and after WA different from any other space in that text sample? Your rules are not precise enough. Think about them again and edit your post. Commented May 3, 2011 at 10:15
  • I have no idea what's going on in this question. As Tim said, the example is very different from the "specification", and neither of them makes sense. Commented May 3, 2011 at 10:43

3 Answers 3

2
@" .*?,.*? "

You didn't mention whether spaces are also allowed inside the string. If they aren't, use

@" [^ ]*,[^ ]* "
Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't that only match a single comma in between?
@Damien_The_Unbeliever: No, the dot matches any character, including the comma. So will the negated character class.
0

Use this

 @" \S+?,\S+? "

Sure...

Comments

0

I'd do:

\s[^\s,]+(,[^\s,]+?)+\s

Comments

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.