0

I have a string that can come in different forms - one example below

%VAR('SERVER','DEFAULT') %VAR('LOC','NYC')Run_ServerRestart.ps1

I want to separate them as individual items and am not able to separate out the command in the end as a match. My regex returns 2 matches for the above example while I am looking to get 3 matches. Any ideas on what I maybe doing wrong and how to separate out them as below and if there is a more efficient way to achieve this ? We need to accommodate for spaces anywhere in between as well.

  • %VAR('SERVER','DEFAULT')
  • %VAR('LOC','NYC')
  • Run_ServerRestart.ps1

Here is my regex so far:

/%VAR\(\s*'([^']+)'\s*\)*\,*\s*'([^']+)'\s*\)|/*\\*[\w-]+\s*\.?\S*/g

However, this above regex is not matching some of my examples below.

  • c:\abc\def.txt - should show 1 match
  • %VAR('SERVER','USA')\C:\batch.bat - should show 2 matches - %VAR('SERVER', 'USA') and \c:\batch.bat
  • %VAR('SERVER','NYC') - should show 1 match
  • %VAR('SERVER','NYC') %VAR('APP','NNJ')Run_Command.ps1 - should show 3 matches
  • %VAR('SERVER','NYC') %VAR('APP','NNJ') and Run_Command.ps1
  • %VAR('SERVER','NYC') -File - should show 2 matches - %VAR('SERVER','NYC') and -File
  • /usr/bin/cat - should show 1 match
  • %VAR('SERVER','NYC')BATCH1.bat - should show 2 matches - %VAR('SERVER','NYC') and BATCH1.bat
  • ftp -s:D:\\apps\\scripts\\Intel\\daily_job.ftp - should show 1 match
  • %VAR('SERVER') - should show 1 match
10
  • Is it always in the form where it first sets variables then is followed by a command? %VAR %VAR ... command Are spaces between the %VARs required or optional? %VAR(a,b)%VAR(x,y) Commented Jan 18, 2018 at 20:42
  • There may be a command or sometimes no command at all, just the variables like %VAR('SERVER', 'DEFAULT') Commented Jan 18, 2018 at 20:43
  • So I would define that as one or more vars, optionally followed by a command. Abstractly, that pattern is (var)+(command)? Then var and command need to be broken down to match what a var looks like, and what a command looks like. A var is literal %VAR( quoted_string comma quoted_string ) etc. This is how I break down what needs to be in a regex. Commented Jan 18, 2018 at 20:47
  • @S.Kablar, 1 match means that the entire string comes out as a group. c:\abc\def.txt - should show 1 match - when I use Patterns in java, it should come out as the entire string here without leaving out anything - c:\abc\def.txt Commented Jan 25, 2018 at 19:38
  • @S.Kablar, thank you. wondering why it throws off 'ftp' though. Commented Jan 25, 2018 at 19:59

2 Answers 2

1

Regex: %VAR\([^\)]+\)|[\S]+(?:\s[\S]+)?

Details:

  • [^] Match a single character not present in the list
  • [] Match a single character present in the list
  • + Matches between one and unlimited times
  • * Matches between zero and unlimited times
  • | or
  • (?:) Non capturing group
  • ? Matches between zero and one times
  • \S matches any non-whitespace character (equal to [^\r\n\t\f\v ])
  • \s matches any whitespace character (equal to [\r\n\t\f\v ])

Regex demo

Sign up to request clarification or add additional context in comments.

17 Comments

Pretty close. How do I make the last command include '_', '-' and any extension after a period (if there is one at all) ? Not all commands can be ps1
%VAR('LOC','NYC')%VAR('LOC','NYC')shell_cmd.sh or %VAR('LOC','NYC')%VAR('LOC','NYC') mycommand
Need to accommodate for any spaces anywhere inbetween too
Think I got it using this : /%VAR('([^']+)','([^']+)')|\S+\.?[0-9a-zA-Z]*/g
@Prashanth Updated! You "accommodate" for any spaces anywhere inbetween...
|
0

Assuming they always look exactly like this you can just abuse the ) as a separator

(.*\))\s(.*\))(.*)

Example with explanation here: https://regex101.com/r/CLBFKa/1

2 Comments

It depends on the number of variables
@Prashanth Is there always a space between the variables?

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.