2

I'm working on a Dart program that will parse data from an XML file. Unfortunately, the file is a bit of a mess, so I'm doing a ton of regex on it to get it into decent shape. Since Dart, like Javascript, doesn't have lookbehind functionality, I've been trying to use capturing parentheses along with the method to mimic lookbehind, to no avail. Dart doesn't like the syntax of it at all, just telling me that function is not defined for myClass

My XML file consists of strings in the following format:

<items>Chicken Sauces/ Creamy Curried Chicken Salad w/ Wild Rice</items>

When finished, I want that string to look like:

<items>Chicken Sauces| Creamy Curried Chicken Salad w/ Wild Rice</items>

I've tried calling this on the string: .replaceAll(new RegExp(r'(\w\s*)/(?=\s*\w)'),"$0|$1") but that gives me errors saying "Expected an identifier" for $0 and $1. If anyone could offer me some pointers on how to properly use capturing parentheses, or a method to mimic lookbehind in Dart, along with the current lookahead, I'd be very grateful!

snyderxc
  • 105
  • 1
  • 11
  • Could you specify more exactly what the criteria are for determining whether a slash should be replaced with a vertical bar? The "w/" will be problematic (or maybe that's the point of your question?). – Compeek Jun 15 '13 at 07:14
  • Yes, I want to replace the forward slash with a vertical bar whenever it is not part of a tag (` – snyderxc Jun 15 '13 at 13:04
  • Oh, I now realize that I would have to be using a negative lookbehind with 'w' which I'm not doing. I think the regex would look something like this: `(?<=[a-vx-zA-VX-Z]\s*)/(?=\s*\w)` if I had negative lookbehind. – snyderxc Jun 15 '13 at 13:14
  • Sorry, revised my regex some more: `([a-vx-zA-VX-Z]\s*)/(\s+[a-zA-z])` matches everything I want it to. – snyderxc Jun 15 '13 at 13:43

1 Answers1

3

Assuming your regexp is correct you should use String.replaceAllMapped: .replaceAllMapped(new RegExp(r'(\w\s*)/(?=\s*\w)'), (match) => "${match[0]}|${match[1]}")

You might also be interested in String.splitMapJoin

Compeek already pointed out that the regexp probably won't do what you want, though.

Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
Florian Loitsch
  • 7,698
  • 25
  • 30
  • Thank you, I think this is going to work. I'll test it today (with some changes to my regexp). – snyderxc Jun 15 '13 at 13:14
  • Thanks a lot! This did solve my problem, although I found that I needed to do `(match) => "${match[1]}|${match[2]}"` – snyderxc Jun 15 '13 at 13:56