Calvin
Calvin

Reputation: 88

Regex in multi line config file, not equal to

I've been trying to research this but I am struggling to find a relevant post. We use a config file for one of our services which looks like the following (Note that some of the names have spaces after the hash, and some have parameters):

#GetContactDetails
GetContactDetails.XXXXUser=USERNAME
GetContactDetails.XXXXPassword=PASSWORD
GetContactDetails.TestFlag=Y
GetContactDetails.EndPointURL=http://111.99.111.999/ENVNAME2/endpoint.xx
GetContactDetails.Timeout=40000
GetContactDetails.LoggingFlag=Y

# GetContactDetails
GetContactDetails.XXXXUser=USERNAME
GetContactDetails.XXXXPassword=PASSWORD
GetContactDetails.TestFlag=Y
GetContactDetails.EndPointURL=http://111.99.111.999/ENVNAME2/endpoint.xx
GetContactDetails.Timeout=40000
GetContactDetails.LoggingFlag=Y

# GetContactDetails Parameters
GetContactDetails.XXXXUser=USERNAME
GetContactDetails.XXXXPassword=PASSWORD
GetContactDetails.TestFlag=Y
GetContactDetails.EndPointURL=http://111.99.111.999/ENVNAME2/endpoint.so
GetContactDetails.Timeout=40000
GetContactDetails.LoggingFlag=Y

Essentially there are 4 different variations:

#servicename
# servicename
#servicename parameters
# servicename parameters

I want to be able to run a replace on this text file, leaving me with the names of each service (Just the lines beginning with #). Meaning I need to identify anything that IS NOT equal to the patter of the # lines. All the combinations I have tried,I can't get it to identify everything else.

Any suggestions?

P.S. (from comments)

I was hoping to use regex as there is an existing Java service which can replace, using regex. To go down that route would require writing a Java service and also deploying that, albeit not too big of a task but I'd like to avoid it if at all possible.

Upvotes: 3

Views: 236

Answers (4)

Calvin
Calvin

Reputation: 88

The following appears to be working fine for the intended purpose. Posting here in case it becomes useful for anyone browsing this question. Thanks for all the help & advice.

[^#].*\..*

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

In Java, the best approach for this task is reading the file line by line and collect lines starting with #.

If you want to apply a regex to the whole file contents, it should be

.replaceAll("(?m)^(?!#).*[\r\n]*", "")

See the regex demo

Explanation:

  • (?m)^ - start of a line ((?m) is the inline modifier making ^ match a beginning of a line, not string)
  • (?!#) - makes sure the first symbol on the line is not # (if it is, no match is returned)
  • .* - zero or more symbols other than a newline
  • [\r\n]* - zero or more CR or LF symbols (in Java 8, it can be replaced with "\\R*")

For your exact environment, I think you can use

[^#].*

And - to also match empty strings - if they can be removed at all there:

^$

You say [^#].*\..* is working for you. Here is what it actually does:

  • [^#] - matches a char other than #
  • .* - matches 1 or more chars other than a newline as many as possible up to the last
  • \. - literal .
  • .* - matches 1 or more chars other than a newline up to the end of line.

Upvotes: 1

Michał M
Michał M

Reputation: 618

Another solution:

#\s?(\w+)\s?(\w+)?

in notepad++ ctrl+h->paste code -> replace with ' '

Upvotes: 1

Bohemian
Bohemian

Reputation: 425073

Search: ^[^#].*[\r\n]*
Replace: <blank>

The key part is ^[^#], which means "the first character is not a hash"

You need the [\r\n]* to also match the newline (dot does not match newline chars), otherwise you'll be left with blank lines (instead of deleting them).

Upvotes: 1

Related Questions