Regular Expression Pattern Question
I'm building up a file URL String in my program. I'd like to remove all occurrences of "\\" in a String with "/" using the java.lang.String.replaceAll method:
String fileName = "junk.txt";
static String DEFAULT_FILE_URL_ROOT = "file:/" + System.getProperty("user.dir") + "/";
String schemaLocation = DEFAULT_FILE_URL_ROOT + fileName;
schemaLocation.replaceAll(java.io.File.separator, "/");But when I run this, I get the following exception:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
^
at java.util.regex.Pattern.error(Pattern.java:1489)
at java.util.regex.Pattern.compile(Pattern.java:1281)
at java.util.regex.Pattern.<init>(Pattern.java:1030)
at java.util.regex.Pattern.compile(Pattern.java:777)
at java.lang.String.replaceAll(String.java:1710)
at forum.jdom.example.DOMValidator.main(Unknown Source)What's the regex pattern I need to match "\\"? I can't find it. - MOD
The file seperator is a string which represents the single character which is a backslash (in windows.)
A single backslash in regex is an escape charater which escapes the next character. But there is no next character in your expression so it doesn't work. You need to replace what you have with the following...
And if you really insist on using the constant then the expression would be...
"\\" + java.io.File.separator
Similar Messages
-
White space in regular expressions (Pattern class)
Hello,
I have to check if a String contains ONLY the following characters: a-z, A-Z, ' and ( ) .
The regular expression that I wanted to use with the Pattern class was [[a-zA-Z][\u0027][\u0028][\u0029][\s]] .
Now, my compiler (Eclipse) doesnt recognize \s as an expression. Is this the proper expression for a white space?
Also, in this notation, will the Pattern check for the order of the characters, cause the order isn't supposed to matter.
I would appreciate any help you could give me on this subject.
With Best Regards, RoderickI'm not a regex expert, but I don't see any of the regex gurus online, and this I can tell you.
my compiler (Eclipse) doesnt recognize \s as an expressionYou need to escape the backspace in a String literal. Use"\\s"
in this notation, will the Pattern check for the order of the charactersFor that I think (note:think, not know) you need to put the entire set of characters to be matched in one character class. Could you try this and post back whether it works for your requirement?"[^a-zA-Z'()\\s]"Note the negation operator at the start of the characer class, which will match positive for any character not in the character class.
db
edit You can test your regex here:
{color:0000ff}http://www.dotnetcoders.com/web/Learning/Regex/RegexTester.aspx{color}
but remember to double the backslashes whe you include it in your java code as a String literal.
Edited by: Darryl.Burke -
Jakarta regular expression package question
hi everyone,
I'm trying to figure out how to use the jakarta regular expression package to return an array of matches ?
For example let's say i had StringBuffer object consisting of say 30,000 characters and I needed to return all the matches that started with < and ended with > (like all the xml tags )
anybody know how I could use the package to return an array of matches ?
thanks in advance
stevokay -- perhaps i should maybe rephrase my question for this package -- perhaps it is more popular ?
anyone know how i can pass a regular expression to it and retrieve a String array
In my specific situation , I have a StringBuffer with about 10,000 characters in it ?
any ideas how i would use regular expressions to look through this Stringbuffer object
thanks
stephen -
-Split Regular expression pattern
Hi all,
I came across the below example in Lee Holmes' PowerShell Cookbook 3rd editon:
"Hello World" -split "He(ll.*o)r(ld)"
and the output is:
llo Wold
Even though I do understand Regular Expressions to some degree (where I can comfortably write a reasonably simple pattern for -replace operator or Select-String cmdlet for example), I am struggling to understand the RE pattern above and what it's trying
to achieve in the -split context. What are the 2 groupings trying to achieve here while splitting a text? What I want to know is the literal translation of the pattern above in clear English words.
Any thoughts?I guess that explains it clearly now. In fact that's what I wrote above yesterday. So parenthesis in -split operator pattern is not a grouping construct, instead a 'preserving' construct, saving it from consumption by the split operation.
Therefore, following -match operator pattern-semantics to understand -split operator will mislead us here.
But then again, I have troubles understanding the below pattern
"Hello World" -split "(He(ll.*o)r(ld))"Hello World
llo Wo
ld
However, I think I will leave it at this stage. The main thing I wanted to know was what those parenthesis were there for and now I have the answer. It did not really makes sense to group a pattern for -split operator hence I started this thread.
I think that's all I wanted to know. Thank you. -
I currently have a RegExp pattern set up with the expression (\w\s\!\$/\(\)\&\.\+\-]+) (there is a [ between the ( and \w but it was showing HTML: instead of [ (maybe the problem?)) but when I try to compile the page, I get an XML-20201: (Fatal Error) Expected name instead of \ error
Does anybody have any idea what I need to change in the regular expression to get it to run in Jdeveloper?
Thanks
Andy<messageTextInput id="${bindings.ItemCodesCodeDescription.path}" model="${bindings.ItemCodesCodeDescription}" required="yes" readOnly="${jhsUserRoles['ITEMS_RO']}" promptAndAccessKey ="Des&cription" rows="1" maximumLength="50" columns="30">
<onSubmitValidater>
<regExp pattern="(\w\s\!\$/\(\)\&\.\+\-]+)"/> //again, [ between ( and \w
</onSubmitValidater>
</messageTextInput>
It's part of a Description textInput, we want to limit the use of characters because it will be saved to our database -
Regular expression usage question
Hi there.
I have a 200 bytes EBCDIC variable record which I need to break down into fields. Fields are positional and are either text, binary numbers, packed-decimal and 64bytes long numbers.
My question is. Can regular expression handle this complex data.
I want to isolate each field into their corresponding format. EBCDIC into ASCII text, binary into java Integer and so on.
The reason for using reqular expression is because the record format could change and regular expression would be easier to modify without having to change the code.
Your words of advice are highly appreciated.
Please advice.
Regards,
UlisesRegular expressions? I don't think so.
If you have a situation where positions 1-3 might be a binary number like client number, and the format might change so it moves to positions 12-14, then you could certainly write a record-format class to encapsulate that sort of information. In fact that would be a very good idea. But I can't imagine how a regular expression would help in getting a number out of three bytes, for example. -
Regular Expressions (Pattern/Matcher) --- Help
Hi,
I have an regex i.e. Pattern.compile("([0-9])D([0-9])'?(?:([0-9]+)\")?([NSEW])").{code}
It has to exactly match the input e.g *45D15'34"N*
I need to retrieve the values based on grouping.
Group1 = 45 (degree value)
Group2 = 15 (minutes value)
Group3 = 34 (seconds value) ----> this is a non-capturing group
Group4 = N (directions)
The regex works fine for most of longitude/latitude value but I get a StackOverFlow for some. There is a known bug on this http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507
According to the bug report, they have said that are many different regex that can trigger the stack overflow....even though the length of my input is not as long as the one posted on the bug report.
I was wondering if anyone could suggest a different way of writing the regex above to avoid the stack over flow.
Thank you in advanceHi,
I missed the '+' in my original regex Pattern.compile("([0-9]+)D([0-9]+)'?(?:([0-9]+)\")?([NSEW])"){code}.
I have also tried {code} Pattern.compile("(\\d+)D(\\d+)'?(?:(\\d+)\")?([NSEW])");And, the other 2 expressions as suggested by you.
The problem happens when Durham Lat=35D52N Lon=78D47W value is selected from a Jtree(the values are parsed from a xml file to the tree - the xml file has a bout 800 longitude/latitude elements for different cities in the US). It does not happen for other values and If I increment the degree or min by, then the expression works. I am not sure how else i could re-write this exp.
Below is the snippet of the xml file:
<State name="NORTH CAROLINA">
<City name="Asheville AP" Lat="35D26'N" Lon="82D32'W"/>
<City name="Charlotte AP" Lat="35D13'N" Lon="80D56'W"/>
<City name="Durham" Lat="35D52'N" Lon="78D47'W"/>
<City name="Elizabeth City AP" Lat="36D16'N" Lon="76D11'W"/>
<City name="Fayetteville, Pope AFB" Lat="35D10'N" Lon="79D01'W"/>
<City name="Goldsboro,Seymour-Johnson" Lat="35D20'N" Lon="77D58'W"/>
<City name="Greensboro AP (S)" Lat="36D05'N" Lon="79D57'W"/>
<City name="Greenville" Lat="35D37'N" Lon="77D25'W"/>
<City name="Henderson" Lat="36D22'N" Lon="78D25'W"/>
<City name="Hickory" Lat="35D45'N" Lon="81D23'W"/>
<City name="Jacksonville" Lat="34D50'N" Lon="77D37'W"/>
<City name="Lumberton" Lat="34D37'N" Lon="79D04'W"/>
<City name="New Bern AP" Lat="35D05'N" Lon="77D03'W"/>
<City name="Raleigh/Durham AP (S)" Lat="35D52'N" Lon="78D47'W"/>
<City name="Rocky Mount" Lat="35D58'N" Lon="77D48'W"/>
<City name="Wilmington AP" Lat="34D16'N" Lon="77D55'W"/>
<City name="Winston-Salem AP" Lat="36D08'N" Lon="80D13'W"/>
</State>
public final class GeoLine {
/* Enum for the possible directions of longitude and latitude*/
public enum Direction {
N, S, E, W;
public boolean isLongitude() {
return (this == E || this == W);
public boolean isLatitude() {
return (this == N || this == S);
public Direction getCanonicalDirection() {
if (this == S) {
return Direction.N;
} else if (this == W) {
return Direction.E;
} else {
return this;
private final int degree;
private final int minute;
private final int second;
private final Direction dir;
/* Recognizes longitude and latitude values that has degrees, minutes and seconds i.e. "45D15'34"N
* or "45D1534"N. The single-quotes for the minutes is optional. And, for the moment we do not support seconds
* validation although ilog library returns the longitude/latitude with second when NEs and Sub-networks are
* dragged and dropped on the map.*/
private static final Pattern PATTERN = Pattern.compile("([0-9]+)D([0-9]+)'?(?:([0-9]+)\")?([NSEW])");
public GeoLine(int degree, int minute, Direction dir) {
this(degree, minute, 0, dir);
public GeoLine(int degree, int minute, int second, Direction dir) {
Log.logInfo(getClass().getSimpleName(), "PAU degree: " + degree + " minute: " + minute + " second: " + second + " direction: " + dir);
verifyLongitudeLatitude(degree, dir);
verifyMinute(degree, minute, dir);
this.degree = degree;
this.minute = minute;
this.second = second;
if (this.degree == 0 && this.minute == 0 && this.second == 0) {
this.dir = dir.getCanonicalDirection();
} else {
this.dir = dir;
public Direction getDirection() {
return dir;
public int getMinute() {
return minute;
public int getDegree() {
return degree;
public int getSecond() {
return second;
public static GeoLine parseLine(String location) {
* Matcher class will throw java.lang.NullPointerException if a null location
* is passed, null location validation is not needed.
Matcher m = PATTERN.matcher(location);
if(m.matches()) {
int deg;
int min;
int second;
Direction direction;
deg = Integer.parseInt(m.group(1));
min = Integer.parseInt(m.group(2));
if (m.group(3) == null) {
second = 0;
} else {
second = Integer.parseInt(m.group(3));
direction = Direction.valueOf(m.group(4));
return new GeoLine(deg, min, second, direction);
} else {
throw new IllegalArgumentException("Invalid location value. Expected format XXDXX'XX\"[NSEW] " + location);
private void verifyMinute(int deg, int min, Direction direction) {
/* This validation is to make sure that minute does not exceed 0 if maximum value for latitude == 90
* or longitude == 180 is specified */
int maxDeg = direction.isLatitude() ? 90 : 180;
if(min < 0 || min > 59) {
throw new NumberFormatException("Minutes is out of range. Value should be less than 60: " + min);
if (deg == maxDeg && min > 0) {
throw new NumberFormatException("Degree value " + deg + "D" + direction + " cannot have minute exceeding 0: " + min);
private void verifyLongitudeLatitude(int valDeg, Direction valDir) {
int max = valDir.isLatitude() ? 90 : 180;
if(valDeg < 0 || valDeg > max) {
throw new NumberFormatException("Degree " + valDeg + valDir + " is invalid");
public final boolean isLongitude() {
return dir.isLongitude();
public final boolean isLatitude() {
return dir.isLatitude();
@Override
public final String toString(){
if(minute < 10){
return degree + "D0" + minute + dir;
} else {
return degree + "D" + minute + dir;
@Override
public boolean equals(Object obj) {
if (obj instanceof GeoLine) {
GeoLine other = (GeoLine) obj;
return (this.degree == other.degree && this.minute == other.minute && this.second == other.second && this.dir == other.dir);
return false;
@Override
public int hashCode() {
int result = 17;
result = result * 37 + degree;
result = result * 37 + minute;
result = result * 37 + second;
result = result * 37 + dir.hashCode();
return result;
}Thank you again. -
Hi
I have to check for for some files in a file system if all the required files exists then next logic continues
for ex: String pattern = "Hello(A|B|C|D|E|F|G).txt";
for (int i =0; i < children.length; i++)
if (Pattern.matches(pattern,children))
set.add(children[i]);
above code sets the elements to set if they exists but does't check if some file is missing.
How do i check my condition for all matches, if not its should exit.
any help whould be appreciatedI think this is essentially the same as his other question.
http://forum.java.sun.com/thread.jspa?threadID=709248 -
Regular Expression Pattern maching class to Validate Alphanumeric String
Hi
MY DB version is Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
I wanted to use the pattern maching classes given by oracle to check whether a given input string is alphanumeric or not.
Oracle 10g documentation says about the classes like [:digit:], [:alnum:] and many others.
http://www.dba-oracle.com/t_regular_expressions.htm
However these classes seems not to be working with my DB version.
Can someone tell me the DB version in which these classes works ?
Below is the code I am using to validate a input string.
SQL> CREATE OR REPLACE FUNCTION fn_is_alphanumeric
2 (
3 pi_value IN VARCHAR2
4 )
5 RETURN VARCHAR2
6 IS
7 lv_length NUMBER;
8 BEGIN
9 lv_length := length(pi_value);
10 IF ( REGEXP_LIKE(pi_value,'[:alnum:]{'||lv_length||'}')) THEN
11 RETURN 'TRUE';
12 ELSE
13 RETURN 'FALSE';
14 END IF;
15 END fn_is_alphanumeric;
16 /
Function created.
SQL>
SQL>
SQL> SELECT fn_is_alphanumeric('abc123') alpha FROM DUAL;
ALPHA
FALSE
SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION fn_is_alphanumeric
2 (
3 pi_value IN VARCHAR2
4 )
5 RETURN VARCHAR2
6 IS
7 lv_length NUMBER;
8 BEGIN
9 lv_length := length(pi_value);
10 IF ( REGEXP_LIKE(pi_value,'[A-z0-9]{'||lv_length||'}')) THEN
11 RETURN 'TRUE';
12 ELSE
13 RETURN 'FALSE';
14 END IF;
15 END fn_is_alphanumeric;
16 /
Function created.
SQL> SELECT fn_is_alphanumeric('abc123') alpha FROM DUAL;
ALPHA
TRUECharacter classes are working fine with regexp engine in your database version - simply change your function definition to:
CREATE OR REPLACE FUNCTION fn_is_alphanumeric
pi_value IN VARCHAR2
RETURN VARCHAR2
IS
lv_length NUMBER;
BEGIN
lv_length := length(pi_value);
IF ( REGEXP_LIKE(pi_value,'[[:alnum:]]{'||lv_length||'}')) THEN
RETURN 'TRUE';
ELSE
RETURN 'FALSE';
END IF;
END fn_is_alphanumeric;
/(brackets should be doubled as compared with your original version).
You can use both , range-based and class-based expressions, but you have to keep in mind, that range-based are nls dependant as opposite to class-based.
SQL> alter session set nls_sort=binary;
Session altered.
SQL> with t as (
2 select 'üäö123' s from dual
3 )
4 select
5 case
6 when regexp_like(s,'^[a-zA-Z0-9]+$') then 'Alphanumeric'
7 else 'Not Alphanumeric'
8 end range,
9 case
10 when regexp_like(s,'^[[:alnum:]]+$') then 'Alphanumeric'
11 else 'Not Alphanumeric'
12 end class
13 from t
14 /
RANGE CLASS
Not Alphanumeric Alphanumeric
SQL>
SQL> alter session set nls_sort='GERMAN';
Session altered.
SQL> with t as (
2 select 'üäö123' s from dual
3 )
4 select
5 case
6 when regexp_like(s,'^[a-zA-Z0-9]+$') then 'Alphanumeric'
7 else 'Not Alphanumeric'
8 end range,
9 case
10 when regexp_like(s,'^[[:alnum:]]+$') then 'Alphanumeric'
11 else 'Not Alphanumeric'
12 end class
13 from t
14 /
RANGE CLASS
Alphanumeric AlphanumericBest regards
Maxim -
Regular Expressions (Pattern/Matcher)
Hi,
I have an regex i.e. *Pattern.compile("([0-9]+)D([0-9]+)'?(?:([0-9]+)\")?([NSEW])").*
It has to exactly match the input e.g *45D15'34"N*
I need to retrieve the values based on grouping.
Group1 = 45 (degree value)
Group2 = 15 (minutes value)
Group3 = 34 (seconds value) ----> this is a non-capturing group
Group4 = N (directions)
The regex works fine for most of longitude/latitude value but I get a StackOverFlow for some. There is a known bug on this [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507]
I was wondering if anyone could suggest a different way of writing the regex above to avoid the stack over flow.
Thank you in advancecienaP wrote:
Hi,
I have reposted the topic. Thank you for letting me know, this is my first time.You didn't need to create a [new thread|http://forums.sun.com/thread.jspa?threadID=5416548&messageID=10865976#10865976]! You could just have posted a response containing your regex. I shall lock this thread. -
Unable to parse control character using regular expression & Pattern class
I am trying to parse the Ctrl-X character from a string using the Pattern class but for some or the other reason it is not finding it.
Here is the code that I wrote,
Pattern p=Pattern.compile("\\cX");
Matcher m=p.matcher(str);
System.out.println(str+": "+m.find());
the result is prop^Xau,bu,ca,en,fe,fi,ge,hj,ma,pe,re,sh,sr,tr^Yto^Xym^Yfmt^Xh : false
where, str= prop^Xau,bu,ca,en,fe,fi,ge,hj,ma,pe,re,sh,sr,tr^Yto^Xym^Yfmt^Xh
Thanks,
Arunrajarunraj.nair wrote:
I am trying to parse the Ctrl-X character from a string using the Pattern class but for some or the other reason it is not finding it.
Here is the code that I wrote,
Pattern p=Pattern.compile("\\cX");
Matcher m=p.matcher(str);
System.out.println(str+": "+m.find());
the result is prop^Xau,bu,ca,en,fe,fi,ge,hj,ma,pe,re,sh,sr,tr^Yto^Xym^Yfmt^Xh : false
where, str= prop^Xau,bu,ca,en,fe,fi,ge,hj,ma,pe,re,sh,sr,tr^Yto^Xym^Yfmt^XhWhat does str.length() give you? And posting, and trying with, a (much) shorter String might help you and others here to more easily see the problem.
db -
Regular expression, Pattern.matcher() method
Pattern.matcher() method start finding a new match from the end of the previous matcher.
Can Pattern.matcher() method start finding a new match from the second letter of the previous match?
For example, this piece of code will give a result as :
find 1 --- 3
find 4 --- 6
+++++++++++++++++++++++++++++++++++++++++++++++++++
Pattern pattern = Pattern.compile("aaa");
Matcher matcher = pattern.matcher("aaaaaaa");
while(matcher.find())
int from = matcher.start() + 1;
int to = matcher.end();
System.out.println("find " + from + " --- " + to);
++++++++++++++++++++++++++++++++++++++++++++++++++++
How can I change the code so the programme can give a result as:
find 1 --- 3
find 2 --- 4
find 3 --- 5
find 4 --- 6
find 5 --- 7
Thanks very much in advance.Check out the API there are Two find methods
public boolean find() - This method starts at the beginning of the input sequence or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.
public boolean find(int start) Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
so you could do it something like this:
int startIndex = 0
while (matcher.find(startIndex)){
int from = matcher.start() + 1;
int to = matcher.end();
System.out.println("find " + from + " --- " + to);
startIndex = ?????
} -
Regular expressions Pattern Matching
Test Data
1. xyz<<<testdata>>>123
2. zzz<<<test<<<data>>>ssf
3. sss<<<test<data>>>sffsd
Expected Result
1. <<<testdata>>>
2. <<<data>>>
2. <<<test<data>>>
Sql Used:
select REGEXP_SUBSTR('<<<testdata>>>', '(<<<)([^<<<]*)(>>>)', 1, 1) from dual;
For 1 and 2 it gives me the proper value as expected, but for third test data its not giving any result.
Can anyone please help me to get this result??how about this then:
WITH t AS (SELECT 'Vendor <<<Acme Co>>>' col1
FROM dual
UNION
SELECT 'You PO Number <<<12345>>> has been generated.'
FROM dual
UNION
SELECT '<<<Wile e cyote>>>'
FROM dual
SELECT t.col1
, REGEXP_SUBSTR(t.col1, '[^a-zA-Z1-9\s]{3}(([a-zA-Z1-9]|\s)+)[^a-zA-Z1-9\s]{3}') new_col1
FROM t
COL1 NEW_COL1
<<<Wile e cyote>>> <<<Wile e cyote>>>
Vendor <<<Acme Co>>> <<<Acme Co>>>
You PO Number <<<12345>>> has been generated. <<<12345>>> -
Need a Regular Expression pattern in Java
Hi,
Thanks in advance for your help. I have to replace a string if it's not available inside any single quote.
e.g.
name = 'abc12namexyz234' or name='def234namewsr345name' and name like '%ab123name345rt%'
In the above I've to replace "name" with "ApplicantEntity.name" but I don't want to replace the string that is available inside the single quote i.e. 'abc12namexyz234'. The result after the replacement should be:
ApplicantEntity.name= 'abc12namexyz234' or ApplicantEntity.name='def234namewsr345name' and ApplicantEntity.name like '%ab123name345rt%'
I am trying to find a appropriate regex. But still no success on that. Please Help....
Thanks,
Utpal.* matches with any character occuring 0 or more times (except newline characters)
The problem is more likely to be that you really mean any character once or more times (any number of times include 0 times)
To do this you need .+ -
Regular Expression / String question?
Ok, So I want to do a String.split and split by a
(.*)But since parenthese's are regex, how would I search for that without messing up the call?CButz wrote:
so
\(.*\)should work?Yup.
But note that if the regex is Java string literal--that is, in your source code in double quotes, as opposed to being read from user input or a config file, you'll need to double the backslash.
"\\(.*\\)"Also, note that that regex will match (abc)def(ghi) as one group. The abc)def(ghi will match the .*
Maybe you are looking for
-
No alt key access to top menus in full screen mode Ps CS5 Ext?
I'm using the trial version of CS5 on a Win7 machine, after using Ps CS3 for a fair amount of time. When painting in Ps, I am almost always in full screen mode. In CS3, you could be in full screen mode and hit the alt key plus the active letter of
-
Is it possible to restore an iPhone backup from Windows 7 in Macbook Pro? How?
I losy my 1st gen iPhone and the Windows 7 laptop it was synchronized to. I have a full backup of the laptop on an external drive. I can see my iPhone backup files on my external drive. Now I got the iPhone 4S and Macbook Pro. I was hoping to use my
-
Using SpeedGrade LUT in Photoshop
Hello, I am experiencing some problems when using LUTs, generated with Speedgrade, in Photoshop. Basically the idea is to color correct the image in Speedgrade and then exporting the look to use it in Photoshop. The problem is that when I load the LU
-
Can I import into Illustrator and modify a drawing in Appleworks?
Can I import into Illustrator and modify a drawing in Appleworks?
-
Default initialisation of instance vars...
* This program currently prints "s before Super() finished is Hello" * followed by "s after Super() finished is Hello" * If the declaration 'String s' is changed to 'String s = null' * the program prints "s after Super() finished is null" * I know th