Regular Expression to Locate String With Wildcard Characters in a Non-Delimited String

I've checked every single post that I can find on here, but can't figure this out.

I have a string which looks something like this: "ABC1234598901AC"

I'm trying to use a Regular Expression to match "5989" within the string, but I want to be able to match that string even if one of the characters is something different.

To simplify, let's say I am searching the string for "59(Random Character that's not 8)9".

Right now here is my Regular Expression: "59[^8]9" but when I use the Matcher in Java it is not matching at all.

Here's the code I'm using to test this:

Matcher test = Pattern.compile("59[^8]9").matcher("ABC1234598901AC");

if (test.matches())

{

System.out.println(test.start());

System.out.println(test.end());

}

Test.matches() is never evaluating to true.

Any help is appreciated, thanks!

Answers


You want to use test.find(), not test.matches().

Matcher.matches() requires your pattern to match the entire input (thus you would need to surround the pattern with .* for it to match), whereas Matcher.find() searches the input string for the first (and then subsequent) substring that matches your pattern.


Need Your Help

Can I export part of an HTML page to an SVG image?

html browser svg rendering

I need a vector image of a Wikipedia navbox. Unfortunately, Inkscape can't open the HTML file, and neither Opera nor Chromium can save the page as SVG. Googling, googling, and yet more googling tur...

UITableView over the top

uitableview swift tableview

I have an UITableViewController with an iAD banner on top, under the top bar.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.