0

I have a string, I need to search this string and be able to assign the address details to a variable:

how it looks in the string:

infoone:"infoone"infotwo:"infotwo"address:"123 fake street pretend land"infothree:"infothree"infofour:"infofour" address:"345 fake street pretend land"infofive: "infofive"infosix: "infosix"

How would I use regular expressions to search through this string to lift only the data in the inverted commas after the word address,?

Note: I cannot target the phrase "123 fake street pretend land" as this is just an example used of what might be in the inverted commas.

2
  • Do you have a multiline string (with all lines in it)? Or every line is a separate string? Commented Jun 6, 2012 at 11:10
  • Its actually just one rather huge string, I've changed the question to show this. Commented Jun 6, 2012 at 11:49

3 Answers 3

2

This is a fine regex

^address:"([^"]*)

This is it in php with the option so that ^ matches at the beginning of the line and we lift out group 1

preg_match_all('/^address:"([^"]*)/m', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[1];

Update 1

preg_match_all('/^address:"([^"]*)/m', $subject, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
    for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
        # Matched text = $result[$matchi][$backrefi];
    } 
}

Update 2

With the new sample input just leave at the ^ in the beginning so it becomes

address:"([^"]*)
Sign up to request clarification or add additional context in comments.

6 Comments

One (imaginary) example where this would fail is: address:"a \"123\" house". The OP did not specify if this is even possible, though.
Yes, I can think of more edge cases (newline in string, address in uppercase, etc..) But these should be mentioned as you mention
This seems to be working to an extent, the string I have contains address more than once with differing values for some reason, this is not finding the very first one in the string. I've just edit my question slightly to make what i'm trying to do more clear if that helps.
Without any concrete sample it's difficult to answer. But I added some code to iterated over all matches and the groups
I've just realised that how I displayed the string it originally in the question was not how it looked in the string and have made the necessary changes to show this. This might help.
|
1

Use this regex

$str='infoone:"infoone"infotwo:"infotwo"address:"123 fake street pretend land"infothree:"infothree"infofour:"infofour" address:"345 fake street pretend land"infofive: "infofive"infosix: "infosix"';

preg_match_all("/address:\"(.*)\"/siU",$str,$out);
print_r($out[1]);

Comments

1

Well, the regexp itself would be ^address:"(.*)"$.

Obviously, you'll need to add relevant preg_match() call.

2 Comments

Don't you have to set the option so that ^ matches at the beginning of a line in php?
@buckley no, I don't think any options are necessary.

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.