I am trying to match this string:
www.abcdgbs.com/p/Flights/abcd
with regex
regex = ".*(/Hotels|(/p)?/Flights).*";
pipe separated for matching from multiple regex
I want to get the index where this string is matching (15 in above case) my code is
String test = "www.abcdgbs.com/p/Flights/abcd";
String regex = ".*(/Hotels|(/p)?/Flights).*";
Pattern patt = Pattern.compile(regex);
Matcher m = patt.matcher(test);
System.out.println(m.find() ? m.start(1) : -1);
It is ignoring /p and matching from Flights giving me index as 17, I want this to first match with bigger string ie /p/Flights
first and give index as 15.
I want to use same regex for matching www.abcdgbs.com/Flights/abcd as well.
Remove .*
from your regex and match the text that you want:
String regex = "(/Hotels|(/p)?/Flights)";
With greedy .*
in your regex before these keywords, regex engine is attempting to match longest string before any of those keywords as your (/p)?
is optional.
So with .*
your first captured group is /Flights
but with .*
removed your first captured group is /p/Flights
.
You will get 15
printed now.
Here is my extended version:
(?:(?:(?P<PROTOCOL>https?):)?//)?(?P<DOMAIN>.+?)/(?P<PREFIX>p/)?(?P<TYPE>Flights|Hotels)(?P<SUFFIX>/.+)?
See in colors here: https://regex101.com/r/wD7aR7/2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.