-2

I am trying to write a Regex in Java. My ask is I need to split string by '/' and get the last entry.

Looking at the discussion REGEX IF THEN ELSE Statement managed to write regex which is working succesfully in php i.e expression: \S*^.?((?(?=.?(\b(?:/)\b).?)\1|.)).*?$

Inputs: /etc/audit/auditd.conf, /etc/audit/rules.d/audit.rules, /etc/audisp/plugins.d/syslog.conf, /etc/httpd/conf/httpd.conf, httpd.conf, rsyslog.conf

Respective outputs: (All in Group 1) auditd.conf, audit.rules, syslog.conf, httpd.conf, httpd.conf, rsyslog.conf. but when trying to write the same in java,

same expression is returning "? The preceding token is not quantifiable" error at (?( usage in the expression, upon escaping ? with \, error is gone but string is not returning last string split by '/'. Please help

brombeer
  • 8,716
  • 5
  • 21
  • 27
  • 3
    Why not just use `split` with `"/"` ? – Scary Wombat Sep 22 '21 at 05:27
  • This expression i have changed as \S*^.*?(:?(?=.*?(?:\/).*?)/.*/|.*).*?$ which is returning outputs ignoring just the last substring(which i want as output ) in Group 1 Inputs: /etc/audit/auditd.conf, /etc/audit/rules.d/audit.rules, /etc/audisp/plugins.d/syslog.conf, /etc/systemd/journald.conf, /etc/rsyslog.conf auditd.conf, audit.rules, syslog.conf, journald.conf. Respective Outputs: /etc/audit/, /etc/audit/rules.d/, /etc/audisp/plugins.d/, /etc/systemd/, /etc/ auditd.conf, audit.rules, syslog.conf, journald.conf. Split till last '/' is working but i want substring after last '/' – hitesh adabala Sep 22 '21 at 05:36
  • Couldn't you just use `/([^/])$`? How would that expression not work? (watch out for multiline flag, else $ wont work as expected) – DownloadPizza Sep 22 '21 at 05:36
  • why do you need an elaborate regex for this? the result of the split method should be sufficient – Stultuske Sep 22 '21 at 05:39
  • @DownloadPizza Added it in if block of previous expression as \S*^.*?(:?(?=.*?(?:\/).*?)/([^/])$|.*).*?$ but didnt help – hitesh adabala Sep 22 '21 at 05:45
  • Hi @Stultuske There is an if else condition in it and in case of if we need to split the string by '/' and get the last substring..... hence expression looks elaborated – hitesh adabala Sep 22 '21 at 05:50
  • `System.out.println("/etc/audit/auditd.conf".split("/")[3]);` – Scary Wombat Sep 22 '21 at 05:51
  • I want strictly regex and no split or replace kind of functions... Hope u all understand.(smile). I need to access this string in an xml file which manipulates expression using java regex – hitesh adabala Sep 22 '21 at 05:57
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 29 '21 at 06:59

1 Answers1

-1

If you explicitly need a regex for this, ([^\/]+$) would work.

A similar question is answered here: Regular Expression for getting everything after last slash

Note: can be done the same using the split method in Java.

This is a resourceful site for regex related experiments :) https://regexr.com/

code99
  • 227
  • 4
  • 11
  • True. Thanks for mentioning it. In that case @hitesh could you please check if this works `[^\\/]+$` – code99 Sep 22 '21 at 06:11
  • hi @IsharaM Just giving the expression [^\/]+$ alone, it looked well except if string is not starting with /, it took all next lines as strings and showed in output. small tweak needed to terminate on finding line end needed may be, but so far it is the most relevant expression. Now i am trying \"(\S.*?([.*\\/]+$)*)\" or \"([.*\\/]+$)\" to fetch auditd.conf from "/etc/audit/auditd.conf" which is my actual requirement. Tried few permutations but no luvk. Can u please help me with required tweak here. – hitesh adabala Sep 22 '21 at 10:09
  • Input: "/etc/audit/auditd.conf" and expressions and results are \"(\S.*?(.)*)\" is returning f, \"(\S.*?(..)*)\" is retuning nf ..... like wise as the number of dots increase, those number of last characters are printed. Here can someone say what to be added to get last string after splitting by '/' (or) last set of characters containing only those among a-z and . Anyways output is from the last entries, guess I just want to place find characters range or split by '/' in second group to get the result. – hitesh adabala Sep 22 '21 at 11:00
  • 1
    @hitesh your requirement is a bit unclear now. I would be grateful if you could explain it. 1) Does the suggested regex give the expected results for your cases? Inputs: `/etc/audit/auditd.conf, /etc/audit/rules.d/audit.rules, /etc/audisp/plugins.d/syslog.conf, /etc/httpd/conf/httpd.conf, httpd.conf, rsyslog.conf` Respective outputs: `auditd.conf, audit.rules, syslog.conf, httpd.conf, httpd.conf, rsyslog.conf` 2) If not, what is missing? – code99 Sep 22 '21 at 14:18
  • the outputs are auditd.conf, audit.rules, syslog.conf, httpd.conf, httpd.confryslog.conf It could not pick a new string start except when it is starting with '/' – hitesh adabala Sep 23 '21 at 07:01
  • i want to know how to consider String in new line everytime as input. For now it is taking Match 1 as: httpd.conf rsyslog.conf auditd.conf audit.rules syslog.conf journald.conf and hence Group 1 (result is) rsyslog.conf auditd.conf audit.rules syslog.conf journald.conf – hitesh adabala Sep 23 '21 at 07:10
  • Woooowwww!! @IsharaM ([^\/|\n]+$) worked!!!!!!!!!! Thanks for this! – hitesh adabala Sep 23 '21 at 07:15
  • Now can u please help with what should be done when input strings are sent within "" Eg : inputs are "/etc/audit.conf", "/etc/systemd/abc.conf", "abc", "def" and the required respective outputs are audit.conf, abc.conf, abc, def – hitesh adabala Sep 23 '21 at 07:18
  • Now i am getting ouputs as audit.conf", abc.conf", abc", def" – hitesh adabala Sep 23 '21 at 07:26
  • @IsharaM \"(\S*?([^\/|\n]+))\" This expression did wonders........ Thanks a lot for help!!! I have included this in a log line where Strings are sent between "" and ask is to find what is the file on which changes are made. Working fine now. Thanks much champ! – hitesh adabala Sep 23 '21 at 10:36
  • @hitesh Great work! You've figured it out :) – code99 Sep 23 '21 at 11:40