-1

I have the following Literal Text

"value":"1001174227","fieldLabel":"A field"},{"value":"THE VALUE I NEED","fieldLabel":"Period"}

My goal was to use regular expressions to extract the string

THE VALUE I NEED

I attempted to match using the regular expression using the standard start and end identifiers by pulling the text in the middle (see my regex below) but it pulls out the whole string. I think this is because of the reoccurring text value.

\"value\":\"(.*)\",\"fieldLabel\":\"Period\"
Bohemian
  • 412,405
  • 93
  • 575
  • 722
Peter H
  • 871
  • 1
  • 10
  • 33
  • `\"value\":\"(.*?)\",\"fieldLabel\":\"Period\"` – Avinash Raj May 26 '16 at 10:05
  • It looks like you're trying to some kind of standard data interchange format like JSON. Is there not a library that's already been written, tested and debugged that can do it for you? – Andy Lester May 27 '16 at 18:32

2 Answers2

0

Capture non-quotes:

value\":\"([^\"]*)\",\"fieldLabel\":\"Period\"

This forces the only match to be the value immediately preceding your target.

The expression [^"] means "any char not a quote", thus restricting the capturing match to only chars between quotes.

See live demo showing target input captured as group 1.

Using a reluctant quantifier .*? won't work, because the first value can still match.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You may be able to simplify it right down to:

"([^"]*)","fieldLabel":"Period"

Depending on the rest of your code, you may or may not need to replace " with \". For example, could you use single quotes (') to define the regex string, and thereby avoid all the ugly backslashes?

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • This will pull out the text "THE VALUE I NEED","fieldLabel":"Period"} – Peter H May 26 '16 at 10:15
  • Yes, and `THE VALUE I NEED` is stored in a capture group. – Tom Lord May 26 '16 at 10:16
  • It's carries on with the entire ending though ? – Peter H May 26 '16 at 10:19
  • Capture groups are stored into variables. A quick google tells me that in `objective-c`, you can access the capture group via `$1`. I can't really provide much more code without knowing what you want to *do* with the captured text, however! – Tom Lord May 26 '16 at 10:33
  • Also notice that the accepted answer above uses the same method of a capture group. – Tom Lord May 26 '16 at 10:34