1

I have following text in my file

autovue.bindir=C:\Program Files (x86)\AV\bin\

autovue.classpath=C:\Program Files (x86)\AV\bin\jvue.jar;C:\Program Files (x86)\AV\bin\jvueserver.jar;C:\Program Files (x86)\AV\bin\jsonrpc4j.jar;C:\Program Files (x86)\AV\bin\log4j-api.jar;C:\Program Files (x86)\AV\bin\jackson.jar;C:\Program Files (x86)\AV\bin\stax2-api-4.2.jar;C:\Program Files (x86)\AV\bin\woodstox-core-5.2.0.jar;C:\Program Files (x86)\AV\bin\log4j-core.jar;C:\Program Files (x86)\AV\bin\log4j-web.jar;C:\Program Files (x86)\AV\bin\jackson.jar;C:\Program Files (x86)\AV\bin\stax2-api-4.2.jar;C:\Program Files (x86)\AV\bin\woodstox-core-5.2.0.jar;C:\Program Files (x86)\AV\bin\jogl.jar;C:\Program Files (x86)\AV\bin\gluegen-rt.jar;C:\Program Files (x86)\AV\bin\SmartMarkupListener.jar autovue.jre=C:\Program Files (x86)\AV\jre\bin\java.exe

I need to get value of autovue.classpath means string after autovue.classpath="..." to the white spaces (i.e.)next line.

I tried with

MatchCollection matched=Regex.Matches(contents,@"classpath=\s*")

&

MatchCollection matched=Regex.Matches(contents,@"classpath=[A-Za-z-0-9]\w+")

but not getting expected.

Which should be the Regex for this in C# language.

Expected result:

C:\Program Files (x86)\AV\bin\jvue.jar;C:\Program Files (x86)\AV\bin\jvueserver.jar;C:\Program Files (x86)\AV\bin\jsonrpc4j.jar;C:\Program Files (x86)\AV\bin\log4j-api.jar;C:\Program Files (x86)\AV\bin\jackson.jar;C:\Program Files (x86)\AV\bin\stax2-api-4.2.jar;C:\Program Files (x86)\AV\bin\woodstox-core-5.2.0.jar;C:\Program Files (x86)\AV\bin\log4j-core.jar;C:\Program Files (x86)\AV\bin\log4j-web.jar;C:\Program Files (x86)\AV\bin\jackson.jar;C:\Program Files (x86)\AV\bin\stax2-api-4.2.jar;C:\Program Files (x86)\AV\bin\woodstox-core-5.2.0.jar;C:\Program Files (x86)\AV\bin\jogl.jar;C:\Program Files (x86)\AV\bin\gluegen-rt.jar;C:\Program Files (x86)\AV\bin\SmartMarkupListener.jar

stackoverflow
  • 82
  • 1
  • 1
  • 10
  • I recommend reading one line at a time with streamreader. You have a multiline text input and in multiline mode you will the return at the end of the line with regex has a different meaning. You matches will get everything after the "classpath" and not just the one line. – jdweng Jan 27 '20 at 13:57
  • Is this an INI file? Maybe you could use something like [ini-parser](https://www.nuget.org/packages/ini-parser/) if so. Regex is probably not the way to go unless this absolutely cannot be done another way. – Kredns Jan 27 '20 at 13:58
  • Please can you provide the string you'd like as a result of the match? – silleknarf Jan 27 '20 at 13:58
  • @silleknarf i have updated the question and added expected result. Thanks – stackoverflow Jan 27 '20 at 14:12

3 Answers3

2
Match match = Regex.Match(contents, @"(?<=autovue.classpath=)[^\r\n]+");

Explanation:

(?<=autovue.classpath=) is a positive look-behind for the text autovue.classpath=. This is not included in the match result.

[^\r\n]+ then matches any number of characters that is not a carriage return or new line, to include in the match.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • Thanks for your ans. it's working fine. I also need to get [jvue.jar;jvueserver.jar;....SmartMarkupListener.jar ] from the obtain result. Cloud you please tell me the regex for same ? – stackoverflow Jan 28 '20 at 10:54
  • @stackoverflow so you want to split the match into an array, separated by the semicolons? – Johnathan Barclay Jan 28 '20 at 11:00
  • yes @johnathan some thing like "MatchCollection matchedAuthorsClasspath1 = Regex.Matches(contents, @"(?<=autovue.classpath=)[^;]+");" but it's not working – stackoverflow Jan 28 '20 at 11:04
  • You could always just do `match.Value.Split(';')` on the existing expression. – Johnathan Barclay Jan 28 '20 at 11:41
  • i am doing String[] s2 = new String[20]; string srt1 = match.ToString(); int count = srt1.Split(';').Length - 1; for (int i = 0; i < count; i++) { s2[i]= match.Value.Split(';').ToString(); } but it's return noting, am i doing right ? – stackoverflow Jan 28 '20 at 12:39
  • I just need [jvue.jar;jvueserver.jar;....SmartMarkupListener.jar ] file name only not complete path. – stackoverflow Jan 28 '20 at 12:41
  • Try [`Path.GetFileName()`](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfilename?view=netframework-4.8). So `var fileNames = match.Value.Split(';').Select(path => Path.GetFileName(path));` – Johnathan Barclay Jan 28 '20 at 12:50
  • Thanks for your ans. but i am looking for file name only not complete path eg. if value of filePaths[0] is "C:\\Program Files (x86)\\AV\\bin\\jvue.jar" then i want jvue.jar only – stackoverflow Jan 28 '20 at 12:54
  • i don't want to use Linq. can you please suggest regex for input "C:\\Program Files (x86)\\AV\\bin\\jvue.jar" to get jvue.jar as result – stackoverflow Jan 28 '20 at 12:57
  • Maybe try this then: https://stackoverflow.com/a/9367263/8126362 – Johnathan Barclay Jan 28 '20 at 13:04
0

If you want to get text only between autovue.classpath and next semicolon (e.g. C:\Program Files (x86)\AV\bin\jvue.jar, then:

MatchCollection matched = Regex.Matches(contents, @"(?<=autovue\.classpath=).+?(?=;)");
Alexander Goldabin
  • 1,824
  • 15
  • 17
0
var match = Regex.Match(text, @"autovue.classpath=(.*)$", RegexOptions.Multiline);

will find the text between autovue.classpath= and the end of the line. You can access it at match.Groups[1].

user78403
  • 352
  • 2
  • 11