Skip to content

Commit c5299ad

Browse files
authored
Merge pull request #207 from DavidVanDeursen/fix-external-url-path-regex
Fix external url path regex
2 parents 21b12fb + 02733c1 commit c5299ad

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/XLParser.Tests/FormulaAnalysisTest.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,14 +636,38 @@ public void ExternalWorkbookUrlPathHttpWithSpaceInDocument()
636636
Assert.AreEqual("Sheet1", references.First().Worksheet);
637637
}
638638

639+
[TestMethod]
640+
public void ExternalWorkbookUrlPathHttpWithTildeInPath()
641+
{
642+
// See [#202](https://github.com/spreadsheetlab/XLParser/issues/202)
643+
List<ParserReference> references = new FormulaAnalyzer(@"='http://example.com/~testfolder/[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList();
644+
645+
Assert.AreEqual(1, references.Count);
646+
Assert.AreEqual(@"http://example.com/~testfolder/", references.First().FilePath);
647+
Assert.AreEqual("Book1.xlsx", references.First().FileName);
648+
Assert.AreEqual("Sheet1", references.First().Worksheet);
649+
}
650+
651+
[TestMethod]
652+
public void ExternalWorkbookUrlPathHttpWithBackslashInPath()
653+
{
654+
// See [#201](https://github.com/spreadsheetlab/XLParser/issues/201)
655+
List<ParserReference> references = new FormulaAnalyzer(@"='http:\\example.com\testfolder\[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList();
656+
657+
Assert.AreEqual(1, references.Count);
658+
Assert.AreEqual(@"http:\\example.com\testfolder\", references.First().FilePath);
659+
Assert.AreEqual("Book1.xlsx", references.First().FileName);
660+
Assert.AreEqual("Sheet1", references.First().Worksheet);
661+
}
662+
639663
[TestMethod]
640664
public void ExternalWorkbookUrlPathHttpWithPortNumberInPath()
641665
{
642666
// See [#194](https://github.com/spreadsheetlab/XLParser/issues/194)
643-
List<ParserReference> references = new FormulaAnalyzer(@"='http://example.com:1234/test/[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList();
667+
List<ParserReference> references = new FormulaAnalyzer(@"='http://localhost:1234/test/[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList();
644668

645669
Assert.AreEqual(1, references.Count);
646-
Assert.AreEqual(@"http://example.com:1234/test/", references.First().FilePath);
670+
Assert.AreEqual(@"http://localhost:1234/test/", references.First().FilePath);
647671
Assert.AreEqual("Book1.xlsx", references.First().FileName);
648672
Assert.AreEqual("Sheet1", references.First().Worksheet);
649673
}

src/XLParser/ExcelFormulaGrammar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public class ExcelFormulaGrammar : Grammar
205205

206206
// Source: http://stackoverflow.com/a/6416209/572635
207207
private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\-.$ @~]+)\\(([^<>\"" /\|?*\\']|( |''))*\\)*";
208-
private const string urlPathRegex = @"http(s?)\://([\p{L}\p{N}-_]+\.[\p{L}\p{N}-_]*)+(:[0-9]+)?/([\p{L}\p{N}\-\.\?\,\'+&%\$#_ ()]*/)*";
208+
private const string urlPathRegex = @"https?\:(//|\\\\)[\p{L}\p{N}\-_.]+(:[0-9]+)?(/|\\)([\p{L}\p{N}\-_.?,'+&%\$# ()~]*(/|\\))*";
209209
private const string filePathRegex = @"(" + windowsFilePathRegex + @"|" + urlPathRegex + @")";
210210
public Terminal FilePathToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFilePath, filePathRegex)
211211
{ Priority = TerminalPriority.FileNamePath };

0 commit comments

Comments
 (0)