![]() ![]() Using the regex string above, here is how we'd split a string using Java: String input = "age: 28, favorite number: 26, \"salary: $1,234,108\"" Using Java and Regex, this should work: String strArray = text.split( ",(?=(*\"*\")**$)") ![]() So now to split on this we'll need to create a regex string that says "split on all comma characters unless it's in between quotes". So our example from above would then look like this: age: 28, favorite number: 26, "salary: $1,234,108" One way to solve this problem is to put quotes around the string that shouldn't be split. Splitting by commas on this would yield: age: 28įor formatting purposes many numbers have commas like this, so we can't really avoid it. So maybe we'd have a string like this: age: 28, favorite number: 26, salary: $1,234,108 Many times when you're parsing text you find yourself needing to split strings on a comma character (or new lines, tabs, etc.), but then what if you needed to use a comma in your string and not split on it? An example of this could be a large number. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |