-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

9
  • 3
    Why not just use split with "/" ? Commented Sep 22, 2021 at 5: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 '/' Commented Sep 22, 2021 at 5:36
  • Couldn't you just use /([^/])$? How would that expression not work? (watch out for multiline flag, else $ wont work as expected) Commented Sep 22, 2021 at 5:36
  • why do you need an elaborate regex for this? the result of the split method should be sufficient Commented Sep 22, 2021 at 5:39
  • @DownloadPizza Added it in if block of previous expression as \S*^.*?(:?(?=.*?(?:\/).*?)/([^/])$|.*).*?$ but didnt help Commented Sep 22, 2021 at 5:45

1 Answer 1

-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/

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

11 Comments

True. Thanks for mentioning it. In that case @hitesh could you please check if this works [^\\/]+$
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.
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 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?
the outputs are auditd.conf, audit.rules, syslog.conf, httpd.conf, httpd.conf<enter>ryslog.conf<enter> It could not pick a new string start except when it is starting with '/'
|

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.