tags:

views:

63

answers:

5

Can somebody tel me how a regex should look like which searches in "V. Guimaraes - FC-Porto" and gives out:

  1. "V. Guimaraes"
  2. "FC-Porto"

EDIT: The Source also could be: "V. Guimaraes - FC-Porto 2:2" "V. Guimaraes - FC-Porto Foo" ...

+1  A: 

You could just split the string at " - " with your programing language's basic string functions. This way you don't even need any regular expressions.

sth
There is more Text after it, sorry missed to post it.
mullek
A: 

how about (.*) - (.*)?

phlip
the problem is, that after it there are additional infos e.g.:
mullek
"V. Guimaraes - FC-Porto 2:2" OR "V. Guimaraes - FC-Porto Foo"
mullek
@mullek: You should edit this information into your question. It is very important information when constructing a regular expression (or any other approach for parsing this data). In fact, it would be very useful to see a couple of full lines that you want to extract information from, along with the information you want to extract.
eldarerathis
A: 
([\S\W]*)[\s]{1}-[\s]{1}([\S\W]*)

And you can fetch answer from group 1 and group 2 $1:$2

Konoplianko
A: 

Search for: "(V.\s+Guimaraes)\s*-\s*(FC-Porto\b).*?"\s+"\1\s*-\s*\2.*?"

Replace with: "$1"\r"$2"

So, if your are using PHP, it will be:

  $result = preg_replace('/"(V.\s+Guimaraes)\s*-\s*(FC-Porto\b).*?"\s+"\1\s*-\s*\2.*?"/', '"$1"\r"$2"', $sourcestring);

The key is $1 $2 which hold the first and the second result. \r means return characters (enter), you can replace it with anything you wish, e.g. a space.

Next time, please mention regex library or application you are using.

Vantomex
A: 

Use the split function of your preferred language to do this. eg Python

>>> s="V. Guimaraes - FC-Porto 2:2"
>>> s.split("-",1)
['V. Guimaraes ', ' FC-Porto 2:2']
ghostdog74