Question

I have a string I am parsing with static Replace as in using this: class function Replace(const Input, Pattern, Replacement: string): string; overload; static;

my string is very simple: -c ABCDE -l 3 and I'm trying to replace -c or /c: or /c= or different possible commandline paramaters. Results should be ABCDE 3 or whatever value.

my function is so: P := regexpr.Replace(Arg,'(?i)(-|/|--)(c|l)(\s|:|=)', '');

P and Arg are string variables.

Replacement pattern is nothing. As in ''

I also tried (?i)(-|/|--)(c|l)(\s*|:|=) to match more spaces but in complied application it doesn't work.

this works but doesn't handle spaces P := regexpr.Replace(Arg,'(?i)(-|/|--)(c|l)(:|=)', '');

This part: (?i) for case insensitive.

This part: (-|/|--) similar to TSysCharSet characters but takes 2 characters at a time as in -- part in expression.

This part: (c|l) for looking at my character to specify Value.

This part: (:|=) specifying to use either : or = after my character and before Value

How do I add Space to that? I tried \s also \x20 and [ ]. I also tried with character such as c:(\s) for more spaces than one: \s* none worked so far as P = -c after replacement.

BTW this expression works on RegexBuddy as it supposed to.

EDIT1: I am replacing: -c, --c, /c, /C, --C, :, =, /x20, -l, --l, --L, etc...

cmd line is: -c ABCDE -l 3 what i have after replace is ABCDE and 3

EDIT2: As Rob Kennedy pointed out in comment below Arg doesn't have the correct string as it chokes on spaces. I used ParamCount to iterate through commands. I guess I have to parse the whole line without ParamCount. Answer below is a good expression and points out to a nice site too.

EDIT3: the expression was (?i)[-\/]+(c|l)[:=\s]* that works for multiple spaces as well. Moral of the story: ParamStr and ParamCount are good tools but do not account for all possible scenarios. better to use cmdLine for the whole string and parse it manually.

Was it helpful?

Solution

Since Delphi mimics the regular expression classes in the .NET framework, in order to match:

  • -c
  • /c:
  • /c=
  • /l
  • /l=
  • --c AGBCDE (capturing --c without AGBCDE)

Use: (?i)[-\/]+(c|l)[:=\s]?

You can see a working example here: http://regex101.com/r/rS3bD7

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top