Can somebody tel me how a regex should look like which searches in "V. Guimaraes - FC-Porto" and gives out:
- "V. Guimaraes"
- "FC-Porto"
EDIT: The Source also could be: "V. Guimaraes - FC-Porto 2:2" "V. Guimaraes - FC-Porto Foo" ...
Can somebody tel me how a regex should look like which searches in "V. Guimaraes - FC-Porto" and gives out:
EDIT: The Source also could be: "V. Guimaraes - FC-Porto 2:2" "V. Guimaraes - FC-Porto Foo" ...
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.
([\S\W]*)[\s]{1}-[\s]{1}([\S\W]*)
And you can fetch answer from group 1 and group 2 $1:$2
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.
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']