java - Regex to match word surrounded by non-alphanumeric characters -


i want match , find index of word either surrounded space or special characters. example:

to find: test input test : true is#input_ : true isinput : false thisisinputtest: false @test right: true. 

how match , find index. current regex fails: (?i)[^a-za-z0-9]test[^a-za-z0-9]

i think need use lookarounds in case:

(?<!\p{alnum})test(?!\p{alnum}) 

the negative lookbehind (?<!\p{alnum}) fail match if there alphanumeric char present left of test, , negative lookahead (?!\p{alnum}) fail match if there alphanumeric char right after test.

see testing screenshot:

enter image description here

java demo:

string str = "this is#test_ :"; pattern ptrn = pattern.compile("(?<!\\p{alnum})test(?!\\p{alnum})"); matcher matcher = ptrn.matcher(str); while (matcher.find()) {     system.out.println(matcher.start()); } 

alternative way: match , capture search word, , print start position of 1st capturing group:

pattern ptrn = pattern.compile("\\p{alnum}(test)\\p{alnum}"); ... system.out.println(matcher.start(1)); 

see java demo

note in scenario, \p{alnum} consuming pattern, , in edge cases, test might not matched.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -