# Match 1 « Match « Java Regex Q&A

Home
Java Regex Q&A
 1 Development 2 find 3 group 4 Match 5 matcher 6 number 7 Operation 8 parse 9 Pattern 10 replace 11 validation 12 word
 Java Regex Q&A » Match » Match 1

### 1. Regex that Will Match a Java Method Declaration    stackoverflow.com

I need a Regex that will match a java method declaration. I have come up with one that will match a method declaration, but it requires the opening bracket of the ...

### 2. Using Regex to generate Strings rather than match them    stackoverflow.com

I am writing a Java utility which helps me to generate loads of data for performance testing. It would be really cool to be able to specify a regex for ...

### 3. How can I add a REGEX match to my J2ME project?    stackoverflow.com

The question pretty much sums it up. Just want to run a regular expression match on a string in J2ME.

### 4. Regex in java question, multiple matches    stackoverflow.com

I am trying to match multiple CSS style code blocks in a HTML document. This code will match the first one but won't match the second. What code would ...

### 5. How do I avoid the implicit "^" and "$" in Java regular expression matching? stackoverflow.com I've been struggling with doing some relatively straightforward regular expression matching in Java 1.4.2. I'm much more comfortable with the Perl way of doing things. Here's what's going on: I ... ### 6. Regex for matching javadoc fragments stackoverflow.com This question got me thinking in a regex for matching javadoc comments that include some specified text. For example, finding all javadoc fragments that include @deprecated: /** * Method1 * ..... * @deprecated * @return */  I manage ... ### 7. Modifying Regular expression to also match .java and .mm files stackoverflow.com I am trying to use the cscope-indexer script. But I want to know how to change the following to include *.mm and *.java files? egrep -i '\.([chly](xx|pp)*|cc|hh)$' | \
sed -e ...

### 8. Print regex matches in java    stackoverflow.com

So I have an IP address as a string. I have this regex (\d{1-3})\.(\d{1-3})\.(\d{1-3})\.(\d{1-3}) How do I print the matching groups? Thanks!

### 9. Java regex to match "t" except when it's "[t" or "t]"    stackoverflow.com

I'm using replaceAll() on a string to replace any letter with "[two letters]". So xxxaxxx to xxx[ab]xxx. I don't want the ones that have already been replaced to be done again ...

### 10. Is it possible to shortcut a regular expression into matching?    stackoverflow.com

I have a few regular expressions which are run against very long strings. However, the only part of the string which concerns the RE is near the beginning. Most of the ...

### 11. How to get multiple regex matches in Java?    stackoverflow.com

How can I find all substrings that match a regex in Java? (Similar to Regex.Matches in .Net)

### 12. Match INI Section Blocks    stackoverflow.com

I'm using regular expressions to try to match section blocks in an INI file. I'm using the recipe given in the book Regular Expressions Cookbook, but it doesn't ...

### 13. Match regexp in java    stackoverflow.com

String text = "[! hello ¡world ¡] otra cosa ¡]";
String pt = "\$!(.*)¡\$";
Matcher mc = Pattern.compile(pt).matcher(text);
while( mc.find() ){
System.out.println(mc.group(1));
}

This code prints hello ¡world ¡] otra cosa. What would be ...

### 14. How do I know if a regexp has more than one possible match?    stackoverflow.com

I am writing Java code that has to distinguish regular expressions with more than one possible match from regular expressions that have only one possible match. For example: "abc." can have several matches ...

### 15. How do I make a regex match for measurement units?    stackoverflow.com

I'm building a small Java library which has to match units in strings. For example, if I have "300000000 m/s^2", I want it to match against "m" and "s^2". So far, I ...

I had this regex in java that matched either an alphanumeric character or the tilde (~) ^([a-z0-9])+|~$ Now I have to add also the characters - and _ I've tried a few combinations, ... ### 17. Java Regex Match Error stackoverflow.com I'm trying the following segment to no avail:  public class test { public static void main(String args[] ){ String password = "s8bgBQYPmUaNjkToXCJLAwAA"; ... ### 18. Why is my regex is not matching? stackoverflow.com I have some lines in a text file like this: ==Text==  I'm trying to match the start, using this: line.matches("^==[^=]")  However, this returns false for every line... little help? ### 19. Match a java main class with regexp stackoverflow.com I'm writing a plugin for emacs to compile and run a java source file with one keystroke. Now I would like to find out the name of the main class or if ... ### 20. string.matches(".*") returns false stackoverflow.com In my program, I have a string (obtained from an external library) which doesn't match any regular expression. String content = // extract text from PDF assertTrue(content.matches(".*")); // fails assertTrue(content.contains("S P E C I ... ### 21. String method to get unknown value that matches regex from string stackoverflow.com In Java is there a way to extract the value of an unknown subset of a string via a regex. For example in the following string "hello world 1234" I want ... ### 22. Is using "\\\\" to match '\' with Regex in Java the most Readable Way? stackoverflow.com I know that the following works but it is not that readable, is there any way to make it more readable in the code itself without the addition of a comment? //Start ... ### 23. Regular expression either/or not matching everything stackoverflow.com I'm trying to parse an HTTP GET request to determine if the url contains any of a number of file types. If it does, I want to capture the entire ... ### 24. Why is this exception thrown when trying to match this regex in Java? stackoverflow.com I'm trying to match a specific string out of a an HTML document and have this regex pattern to grab it: Pattern somePattern = Pattern.compile("var json = ({\"r\":\"^d1\".*});");  However when I try to ... ### 25. Regexp match in Java stackoverflow.com Regexp in Java I want to make a regexp who do this verify if a word is like [0-9A-Za-z][._-'][0-9A-Za-z] example for valid words A21a_c32 daA.da2 das'2 dsada ASDA 12SA89  non valid words dsa#da2 34$

Thanks

### 26. java.util.regex matching anything before expression    stackoverflow.com

I trying to tokenize following snippets by types of numbers:

"(0-22) 222-33-44, 222-555-666, tel./.fax (111-222-333) 22-33-44 UK, TEL/faks: 000-333-444, fax: 333-444-555, tel: 555-666-888"

and
"tel: 555-666-888, tel./fax (111-222-333) 22-33-44 UK"

and
"fax (111-222-333) 22-33-44 UK, ...

### 27. What's up with this regular expression not matching?    stackoverflow.com

public class PatternTest {
public static void main(String[] args) {
System.out.println("117_117_0009v0_172_5738_5740".matches("^([0-9_]+v._.)"));
}
}

This program prints "false". What?! I am expecting to match the prefix of the string: "117_117_0009v0_1" I ...

### 28. method matches not work well    stackoverflow.com

I don't understand why with this regex the method returns false;

Pattern.matches("\\bi", "an is");

the character i is at a word boundary!

### 42. Regular Expression: Not all matches are printed when using System.out.println(m.matches());    stackoverflow.com

I execute the following code:

public static void test() {
Pattern p = Pattern.compile("BIP[0-9]{4}E");

Matcher m = p.matcher("BIP1111EgjgjgjhgjhgjgjgjgjhgjBIP1234EfghfhfghfghfghBIP5555E");
System.out.println(m.matches());
while(m.find()) {
System.out.println(m.group());
}

} What i cannot explain is when the code is ...

### 43. Regular expression containing anchor ( $) when matched with an input shows unexpected result in the following program, Why? stackoverflow.com /* patString: it$
input: this is it
the output at 2 to the match operation at 1 is false, why?
*/

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ...

### 44. Regex for java's String.matches method?    stackoverflow.com

Basically my question is this, why is:

String word = "unauthenticated";
word.matches("[a-z]");

returning false? (Developed in java1.6) Every bit of documentation I've read suggests this should work, as well as \S, ^\s, and other ...

### 45. Java regular expression match    stackoverflow.com

I need to match when a string begins with number, then a dot follows, then one space and 1 or more upper case characters. The match must occur at the beginning ...

### 46. regular expression matching in java    stackoverflow.com

Here is my example file :

lineone one
RUN lineone two
lineone three
RUN lineone four

I want to select all lines not starting with run, here is how I did it :
^([^RUN])

Is it possible to ...

### 47. Java regex return after first match    stackoverflow.com

how do i return after the first match of regular expression? (does the Matcher.find() method do that? ) say I have a string "abcdefgeee". I want to ask the regex engine stop ...

### 48. regular expression to match java class/interface/enum/annotation definition    stackoverflow.com

I created a regular expression bellow to do this. But it also matches comments like //This class blabla...

\\S*.*\\s*(class|interface|enum|@interface)\\s+.*

Would you help me come out a regular expression to match the java class/interface/etc.. ...

### 49. matching string to filenames with regex (java 1.6)    stackoverflow.com

I have a bunch of images with names such as nineDiamonds.gif and aceHearts.gif along with a "Card" class for playing card faces. In the constructor, I want to pass a String of ...

### 50. Java String.matches regex with OR pipe    stackoverflow.com

I am trying to make the following match, but it is not returning true as I would expect:

String attrs = "id|fullname|email|title";
String regex = "fullname|email";
return attrs.matches(regex);

Instead it returns false. Why is this? I ...

### 52. Need java regex to match the below string    stackoverflow.com

What is the java regex to match the below pattern?

<anyString>.<5 or 10>.anyNumber.anyNumber

Here 5 and 10 are numbers.

### 53. Using Java regex want to match only & but not & < etc    stackoverflow.com

Using Java regex pattern I want to match the & symbol. But it should not match &amp;, &lt; etc... (HTML encoding).

### 54. Regex to match ResourceBundle    stackoverflow.com

I need a regular expression that will match the file name of a ResourceBundle, which follows the format name_lo_CA_le.properties. It should only match bundles that have a locale portion in their ...

### 55. REGEX matching problem    stackoverflow.com

My entering string can be in format TIM0.VW0 ( it always starts with TIM or CNT or ENC followed by numbers, then always point and at the end char or chars ...

### 56. Matched fragment when using Lucene's RegexQuery    stackoverflow.com

I'm performing query operations on a Lucene index with a RegexQuery. For example, I do a RegexQuery to obtain all documents containing a URL, using new RegexQuery(new Term("text", "https?://[^\\s]+") (I know, ...

### 57. regular expression matching fully qualified java classes    stackoverflow.com

What is the best way to match fully qualified Java name. Like java.lang.Reflect, java.util.ArrayList, org.hibernate.Hibernate Thanks

### 58. Regular expression to match simple "id" values?    stackoverflow.com

I need regex for a line that starts with two characters followed by 2-4 digits or 2-4 digits followed by "-" and followed by 2-4 digits. Examples:

• AB125
• AC123-25
• BT1-2535
Seems simple , but I got ...

### 59. Java RegEx: Very Specific Match    stackoverflow.com

I'm trying to match a hyphen in all situations except when it is preceded by whitespace and proceeded by non-whitespace.

x-y   //match
x - y //match
x- y  //match
x -y  ...

### 60. matching regex of a particular form    stackoverflow.com

I want to match a regex of the following form

(hex_number) (anything) constant=(true or false)

anything can match any printable character or a space or tab. I am programming in java so I ...

### 61. Regex Match with nested Parens    stackoverflow.com

OK, I don't know if it is possible to write this regex, so I am going to start by askign for help - so far I have had no success. source string:

,convert(varchar(8000), ...

### 62. Why doesn't this string matches my regex?    stackoverflow.com

I have the following regex for capturing links in tags (the tags are given in a string without the angular brackets):

^a .*href=['\"]([^'\"]*)['\"].*

and I have the following string:
a href="/wiki/Hypertext_Transfer_Protocol" title="Hypertext Transfer Protocol"

And ...

### 63. Anyone know how to test an entire String for a match using Java regex?    stackoverflow.com

I would like to search a String for an entire match. In other words, if String s = "I am coding", and I type in that I am searching for ...

### 64. Simple regexp match    stackoverflow.com

What's the correct regex to match such set [1] [2] [3] [4] [23] - where numbers are inside the brackets. (I need to get the brackets though)

### 65. Regexp to match full Exception name    stackoverflow.com

I need a regexp to get the whole exception name incl. dots from stacktrace and it's a whitespace before it.

Time: Sun Apr 10 20:36:57 CEST 2011
Message: java.lang.NullPointerException
at com.hello.World.Hi.initDB(bla.java:273)

...

### 66. How to determine where a regex failed to match    stackoverflow.com

I have tests where I validate the output with a regex. When it fails it reports that output X did not match regex Y. I would like to add some indication ...

### 67. Could java regex match a string which exists or not?    stackoverflow.com

I want to match the following strings:

<a href="http://xyz">xyz</a>
<a href="https://xyz">xyz</a>
<a href="xyz">xyz</a>

and I tried to use the regex but failed:
<a href="((http://|https://|).+)">\1</a>

What is the correct solution to achieve this? Thanks!

### 68. insert string that match regular expression    stackoverflow.com

My program takes a string input from a user. If the input string matches the regular expression then it should be inserted into the arrayList. I wrote that following. But, it does ...

### 69. Java Regex won't match, any explanations?    stackoverflow.com

The regex String :

"[Ff][uU][Nn][Cc] "

Matches input:
"fUnC "

But not:
"func across( a, b )"

And I don't understand why... I'm testing my expressions here: http://www.regexplanet.com/simple/index.html I figured out that I (dumbly) needed my ...

### 70. Regex not to match a set of Strings    stackoverflow.com

How to construct a regex not to contain a set of strings within. For this example, I want to validate the Address Line 1 text box so that it wont contain ...

### 71. Regex doesn't matches    stackoverflow.com

I've a trouble with regexp in such situation: I need to extract (and replace) all dots from such construction:

any_symbols->white_space->x.(or xx. or Xx. or xX. or xy. or yy. etc.)->white_space->any_symbol_not_upper_case_and_not_a_digit

for example
1990 x. abcd ...

### 72. How do you capture and reuse a match with Java regex?    stackoverflow.com

I'm trying to remember the correct notation for doing find-replace regex matches in Java. Say I have the string

    String s = "My name is ''Eric'' and ...

### 73. Matching to one decimal place in Java using regex    stackoverflow.com

Hopefully a quick question. I'm trying to validate a double. Making sure it's positive to one decimal place. Good Values: 0.1, 2.5, 100.1, 1, 54
Bad Values: -0.1, 1.123, 1.12, abc, 00012.1 So I've ...

### 74. What does the regex string "\\p{Cntrl}" match in Java?    stackoverflow.com

I think it's matching all control characters (not sure what "all" might be) but I can't be certain, nor can I find it in any documentation other than some musings in ...

### 75. Regex inverse matching on specific string?    stackoverflow.com

I would like to match the following

• com.my.company.moduleA.MyClassName
• com.my.company.moduleB.MyClassName
• com.my.company.anythingElse.MyClassName
but not the following
• com.my.company.core.MyClassName
My current simple regex pattern is :
Pattern PATTERN_MODULE_NAME = Pattern.compile("com\\.my\\.company\\.(.*?)\\..*")

Matcher matcher = PATTERN_MODULE_NAME.matcher(className);
if (matcher.matches()) {
// will return the ...

### 76. Why does "hello\\s*world" not match "hello world"?    stackoverflow.com

Why does this code throw a InputMismatchException ?

Scanner scanner = new Scanner("hello world");
System.out.println(scanner.next("hello\\s*world"));

The same regex matches in http://regexpal.com/ (with \s instead of \\s)

### 77. How to generate a regular expression at runtime to match a numeric range    stackoverflow.com

I need to generate, at run time, a regular expression that will match a range of numeric values. For example: At run time I may discover that I need a regular expression ...

### 78. Java regular expression longest match    stackoverflow.com

I am having a problem with a generic regex that matches (sort of) a typical string of the form

... "field1" "field2" "field3" "field4" ...

What I want to do is, of course, ...

### 79. Java regular expression to match a lexicographical range of strings    stackoverflow.com

I have looked through all kinds of Java REGEX information and have not found the answer I'm looking for: How do I find all Strings that are between two values (abc and ...

### 80. Why is this regex not matching?    stackoverflow.com

I can't find out why my regex does not match a line when using java's String.matches method - it does match using on online regex tester. Here is the Java-Code: ...

### 81. Regex unordered matches    stackoverflow.com

This feels like it should be an extremely simple thing to do with regex but I can't quite seem to figure it out. I would like to write a regex which ...

### 82. java replaceAll and '+' match    stackoverflow.com

I have some code setup to remove extra spaces in between the words of a title

String formattedString = unformattedString.replaceAll(" +"," ");

My understanding of this type of regex is that it will ...

### 83. java regular expression - match all strings except few strings    stackoverflow.com

I have these strings in an array and I want to iterate this array and find those who match (one line per match check).
The thing is, I can't find the right ...

### 84. Why does my regex containing \d{1,} together with a negative lookahead still match, where it shouldn't?    stackoverflow.com

I'm trying to match a coordinate pair in a String using a Regex in Java. I explicitly want to exclude strings using negative lookahead. to be matched:

558,228
558,228,
558,228,589
558,228,A,B,C

NOT to be matched:
558,228,<Text>

The Regex ^558,228(?!,<).* ...

### 85. Java Regex not matching?    stackoverflow.com

I have this code, but it does not seem to be working.

Pattern pattern=Pattern.compile("IMGURSESSION=([0-9a-zA-Z]*);");
Matcher matcher=pattern.matcher("IMGURSESSION=blahblah; path=/; domain=.imgur.com");
System.out.println(matcher.matches());

Would anyone know why?

### 86. Java tool for matching multiple regular expressions with priorities to multiple strings    stackoverflow.com

I have an unlimited sequence of strings and numerous regular expressions ordered by priorities. For each string in a sequence I have to to find the first matching regular expression and ...

### 87. Why is my string not match()ing?    stackoverflow.com

I have a multiline XML blob in a Java String that I'd like to grep some values out of. I know that regexes and XML mix like crude oil and ...

### 88. getting matching regular expressions in java    stackoverflow.com

String.split(String regex) splits the string around a given regular expression and returns an String array. But I am interested in the regex matches and would like them to be returned as ...

### 89. How to match a set of string in regexp    stackoverflow.com

How do I combine ch..+ and ch..- in regexp effectively without having to scan separately? And are we using matcher in the pattern? My output code is like this:

ch01+
ch01-
ch02+
ch02-
...


### 90. Verify if String matches a format String    stackoverflow.com

In Java, how can you determine if a String matches a format string (ie: song%03d.mp3)? In other words, how would you implement the following function?

/**
* @return true if formatted equals ...

### 92. Regex to fetch all matching values from xml node    stackoverflow.com

Following this question it gives me just 1st match. I want to get all the matching into a string or a string array This is my part of output from which ...

### 93. RegEx Matching Help    stackoverflow.com

Here is the input string:

#video:firstVideo.mp4#
#video:secondVideo.mp4 title:Title of second Video#
#video:thirdVideo.mp4#

I want to match all of the above lines (the title: attribute is optional). However, the Java RegEx below will only match the ...

### 94. String matches regexp    stackoverflow.com

I used perl, unix and java regular expression lot of time, but I'm surprised in java about that:

    "help".matches("^h")


### 95. Why does the Java regex ".*[two].*" match "[one]"?    stackoverflow.com

Why does String.matches return true when two values are in [] brackets?

System.out.println("[one]".matches("(?i).*" + "[two]" + ".*"));
//Why does it return true? Shouldn't "[]" be treated as value?
System.out.println("one".matches("(?i).*" + "two" ...

### 96. Regex (?:[,\\. ]*(&|and) ?) is not matching ', and "    stackoverflow.com

Regex (?:[,\\. ]*(&|and) ?) is not matching ", and ". But when i change the matcher to (?:[,\\.] ?(&|and) ?) the pattern ", and " is getting matched. What is causing ...

### 97. How to write regular matching expression. -Java    stackoverflow.com

I want to write matching expression to read string between parentheses () from a big string. eg: the big string is:-

(something), (something2), (something3)

How can I write matching expression for this to ...

### 98. Java, regular expressions, String#matches(String)    stackoverflow.com

I'm used to regular expressions from Perl. Does anybody know why this doesn't work (eg. "yea" isn't printed)?

if ("zip".matches("ip"))
System.out.println("yea");

Thank you.

### 99. Strange behavior of String's matches() method    stackoverflow.com

I encountered an interesting issue about String's matches(RegExp) method.

assertTrue("33CCFF".matches("[0-9A-Za-z]{6}"));
assertTrue("CC33FF".matches("[0-9A-Za-z]{6}"));
assertTrue("CC3355".matches("[0-9A-Za-z]{6}"));
assertTrue("CC9955".matches("[0-9A-Za-z]{6}"));
assertTrue("CC3366".matches("[0-9A-Za-z]{6}"));
assertTrue("CC3965".matches("[0-9A-Za-z]{6}"));
assertTrue("CC1961".matches("[0-9A-Za-z]{6}"));
assertTrue("CC9999".matches("[0-9A-Za-z]{6}"));
assertTrue("??3966".matches("[0-9A-Za-z]{6}")); // failing
assertTrue("??9965".matches("[0-9A-Za-z]{6}")); // failing
assertTrue("??9966".matches("[0-9A-Za-z]{6}")); // failing

The last 3 assertion is failing unexpectedly. I couldn't find any reasons why this weird behavior ...

### 100. Regular expression never matches    stackoverflow.com

I have the following entries in my log files:

09-22-11 12:35:09 1ms   INFO         ...erChangeSetListener:91         ...