Quantcast
Channel: What is a word boundary in regex? - Stack Overflow
Viewing all articles
Browse latest Browse all 14

What is a word boundary in regex?

$
0
0

I'm trying to use regexes to match space-separated numbers.I can't find a precise definition of \b ("word boundary").I had assumed that -12 would be an "integer word" (matched by \b\-?\d+\b) but it appears that this does not work. I'd be grateful to know of ways of .

[I am using Java regexes in Java 1.6]

Example:

Pattern pattern = Pattern.compile("\\s*\\b\\-?\\d+\\s*");String plus = " 12 ";System.out.println(""+pattern.matcher(plus).matches());String minus = " -12 ";System.out.println(""+pattern.matcher(minus).matches());pattern = Pattern.compile("\\s*\\-?\\d+\\s*");System.out.println(""+pattern.matcher(minus).matches());

This returns:

truefalsetrue

Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>