Regular Expression compilation problem
I am trying to use JDK 1.4's regular expression classes to match text that appears with curly braces (e.g. {field1}) using the following regular expression:
[{]([0-9A-Za-z]+)[}]
I use this same pattern with the org.apache.regexp and org.apache.oro packages without any problem, but get the following error when running it using the new java.util.regex package in JDK 1.4:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected character '?' near index 0
[{]([0-9A-Za-z]+)[}]
^
at java.util.regex.Pattern.error(Pattern.java:1472)
at java.util.regex.Pattern.range(Pattern.java:2052)
at java.util.regex.Pattern.clazz(Pattern.java:1991)
at java.util.regex.Pattern.sequence(Pattern.java:1529)
at java.util.regex.Pattern.expr(Pattern.java:1489)
at java.util.regex.Pattern.compile(Pattern.java:1257)
at java.util.regex.Pattern.<init>(Pattern.java:1013)
at java.util.regex.Pattern.compile(Pattern.java:760)
at Test_jdk14.main(Test_jdk14.java:13)
What am I doing wrong?
Thanks, Tony
Hello,
Let's look at what you've done.
[\\{]([0-9A-Za-z]+)[}]Okay, first you escape the opening curly brace, but you did not escape the closing curly brace.
[\\{]([0-9A-Za-z]+)[\\}]Next, you use the square brackets, but you only have one thing in the brackets, so, they are unnecessary:
\\{([0-9A-Za-z]+)\\}Next, I am going to contract all that stuff inside your remaining square brackets to \\w, since that covers 0-9A-Za-z_
\\{(\\w+)\\}Now, are you expecting any sort of spaces within the curly brackets? If so, you didn't specify them. I'll assume you didn't.
Now you did all this, but it still doesn't match what you thought it would match. Did you use Matcher.matches or Matcher.lookingAt?
If you look at the API for these methods, you'll see that
a) Matcher.matches(...) only returns true if the entire input sequence matches the pattern.
b) Matcher.lookingAt(...) only returns true if the start of the input sequence matches the pattern.
In either of these cases, if your string didn't start with the opening bracket, then the Pattern (in its current form) won't match. For example:
"{text1}" // matches() == true
// lookingAt() == true
"{text2} blah blah" // matches() == false
// lookingAt() == true
"blah blah {text3}" // matches() == false
// lookingAt() == false
"blah blah {text4} blah blah" // matches() == false
// lookingAt() == false
//You would want to use Matcher.find() if you want to find all of these matches.
However, there is a way to find those matches using Matcher.matches() and Matcher.lookingAt(). All you have to do is put .* in front and behind the thing you're looking for.
.*\\{([\\w]+)\\}.*This way, any number of characters can come before or after your pattern, and it will still fit the requirements.
Here's an example:
import java.util.*;
import java.util.regex.*;
public class RegExample
public static void main( String [] args )
Pattern p = Pattern.compile( ".*\\{(\\w+)\\}.*" );
Matcher m = p.matcher( "blah blah {field1} blah blah" );
if ( m.lookingAt() )
System.out.println( "We're looking at it." );
if ( m.matches() )
System.out.println( "It matches." );
m.reset();
if ( m.find() )
System.out.println( "we found the first match" );
System.out.println( "It was: " + m.group(1) );
}
Similar Messages
-
Regular Expression escaping problem!
Hi all,
Well as you know characters such as '*' and '?' are reserved characters in regular expression so I suppose if I want to find the exact character (a '?' for example) in a string, I have to escape it with a backslash:
str=str.replaceAll("\?","a");
In the statement above, I need to replace all question marks with another character but it has a compile error:
Error(116,48): invalid escape character
to my surprise this following code does the job while it should be interpreted as: one occurance of the '\' character or nothing at all:
str=str.replaceAll("\\?","a");
What's the problem? what's the general rule on escaping such characters?I think you're right.
The point is that java first interprets the String and handle its own scape characters then the result would be passed on to reqular expression parser.
so I guess in order to search for one occurance of the '\' character or nothing at all the java string would be:
so that the input to reqular expression parser would be:
while in the first situation:
a "\\?" java string is first trasformed into \? which will be considerd as one occurance '?' in reqular expression point of view...
Message was edited by:
nerssi
Message was edited by:
nerssi -
Regular Expression Validator Problem
Hi,
I have a text area that I want to use a regular expression
validator on to verify that one or more 9 char alphanumeric serial
numbers were entered.
<mx:RegExpValidator
source="{entryField}"
property="text"
flags="gi"
expression="{/[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0- 9][a-zA-Z0-9][a-zA-Z0-9](
|,|\\n|\\r|\\t)/}"
valid="handleResult(event)"
invalid="handleResult(event)"
trigger="{entryField}"
triggerEvent="textInput"
/>
//for testing
private function
handleResult(eventObj:ValidationResultEvent):void {
if (eventObj.type == ValidationResultEvent.VALID)
entryField.setStyle("borderColor", "green");
entryField.setStyle("borderThickness", "2");
else
entryField.setStyle("borderColor", "red");
entryField.setStyle("borderThickness", "2");
The problem is the handler function always comes back
invalid, even when it should be valid, such as: 123456789 a12345678
lk231jkop
Can anyone advise where I might be going wrong?
Thanks!This is weird, i just tried your RegExp using the Adobe's
Live Docs example and it worked perfectly.
Try it at:
http://livedocs.adobe.com/flex/201/langref/mx/validators/RegExpValidator.html
Scroll down to the end of the page, then type your RegExp and
your TestValue and click the Validate button, it works. I am
guessing that maybe it is your
i
flag, try removing it from your validator.
Hope this helps -
Improving Java Regular Expression Compile Time
Hi,
Just wondering if anyone here knows how can i improve the compile time of Java Regular Expression?
The following is fragment of my code which I tired to see the running time.
Calendar rightNow = Calendar.getInstance();
System.out.println("Compile Pattern");
startCompileTime = rightNow.getTimeInMillis();
Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
rightNow = Calendar.getInstance();
endCompileTime = rightNow.getTimeInMillis();
Below is fragment of my regular expression:
(?:tell|state|say|narrate|recount|spin|recite|order|enjoin|assure|ascertain|demonstrate|evidence|distinguish|separate|differentiate|secern|secernate|severalize|tell apart) me (?:about|abou|asti|approximately|close to|just about|some|roughly|more or less|around|or so|almost|most|all but|nearly|near|nigh|virtually|well-nigh) java
My regular expression is a very long one and the Pattern.compile just take too long. The worst case that I experience is 2949342 milliseconds.
Any idea how can I optimise my regular expression so that the compilation time is acceptable.
Thanks in advanceMy regular expression is a very long one and the
Pattern.compile just take too long. The worst case
that I experience is 2949342 milliseconds.Wow, that's pretty pathological. I was going to tell you that you were measuring something wrong, because I had written a test program that could compile a 1 Mb "or" pattern (10,000 words, 100 bytes per) in under 200 ms ... but then I noticed that your patterns have two "or" components, so reran my test, and got over 14 seconds to run with a smaller pattern.
My guess is that the RE compiler, rather than decomposing the RE into a tree, is taking the naive approach of translating it into a state machine, and replicating the second component for each path through the first component.
If you can create a simple hand-rolled parser, that may be your best option. However, it appears that your substrings aren't easily tokenized (some include spaces), so your best bet is to break the regexes into pieces at the "or" constructs, and use Pattern.split() to apply each piece sequentially.
import java.util.Random;
import java.util.regex.Pattern;
public class RegexTest
public static void main(String[] argv) throws Exception
long initial = System.currentTimeMillis();
String[] words = generateWords(10000);
// String patStr = buildRePortion(words);
// String patStr = buildRePortion(words) + " xxx ";
String patStr = buildRePortion(words) + " xxx " + buildRePortion(words);
long startCompile = System.currentTimeMillis();
Pattern pattern = Pattern.compile(patStr, Pattern.CASE_INSENSITIVE);
long finishCompile = System.currentTimeMillis();
System.out.println("Number of components = " + words.length);
System.out.println("ms to create pattern = " + (startCompile - initial));
System.out.println("ms to compile = " + (finishCompile - startCompile));
private final static String[] generateWords(int numWords)
String[] results = new String[numWords];
Random rnd = new Random();
for (int ii = 0 ; ii < numWords ; ii++)
char[] word = new char[20];
for (int zz = 0 ; zz < word.length ; zz++)
word[zz] = (char)(65 + rnd.nextInt(26));
results[ii] = new String(word);
return results;
private static String buildRePortion(String[] words)
StringBuffer sb = new StringBuffer("(?:");
for (int ii = 0 ; ii < words.length ; ii++)
sb.append(ii > 0 ? "|" : "")
.append(words[ii]);
sb.append(")");
return sb.toString();
} -
Regular Expression wierdness - problem with $ character
If I use the following KM code in Beanshell Technology - it works correctly and replaces "C$_0MYREMOTETABLE RMTALIAS, MYLOCALTABLE LOCALIAS, " with "C$_0MYREMOTETABLE_000111 RMTALIAS, MYLOCALTABLE LOCALIAS, "
But when I try to use the same exact code in 'Undefined' technology - it does not match anything in the source string - and does not replace anything.
If I change the regular expression to not use the $ it still does not work.
But if I change the source string to remove the $ - then the regular expression works.
If I use the same code in Beanshell technology - it works fine - but then I can't use the value in a later 'Undefined' technology step.
Does anyone know if the java technology does something special with $ characters when ODI parses the KM code?
Does anyone know if there is a way to use the value from a Beanshell variable in a 'Undefined' technology step?
String newSourceTableList = "";
String sessionNum ="<%=odiRef.getSession("SESS_NO") %>";
String sourceTableList = "<%=odiRef.getSrcTablesList("", "[WORK_SCHEMA].[TABLE_NAME] [POP_TAB_ALIAS]" , ",", ",") %>";
String matchExpr = "(C\\$_\\S*)"; (should end with two backslashes followed by 'S*' - this editor mangles it)
String replaceExpr = "$0_"+sessionNum+ " ";
newSourceTableList = sourceTableList.replaceAll(matchExpr,replaceExpr);
---------------------------------------------------Phases of substitution in ODI:
The way ODI works allows for three separate phases of substitution, and you can use them all. The three phases are:
- First Phase: <% %> You will see these appear in the knowledge moduiles etc and these are substituted on generation. (when you generate a scenario, or tell ODI to execute an interface directly) this phase is used to generate the column names, table names etc which are known from the metadata at that phase.
- Second Phase: <? ?> This phase is substituted when the scenario is instatntiuated as an excution - session generation. At this point, ODI has the additional information which allows it to generate the schema names, as it has resolved the Logical/Physical Schemas through the use of the Context (which is provided for the execution to take place. All the substitutions at this point are written to the execution log.
- Third Phase <@ @> This phase is substituted when the execution code is read from the session log for execution. You will note that anything substituted in this phase is NEVER written to the execution log. (see PASSWORDS as a prime example, you don't want those written to the logs, with the security risks associated with that!)
Anything in <@ @> is always interpreted for substitution by the java beanshell, it does not have to be a Java Beanshell step, it can be any kind of step, it will be interpreted at that run-time point. -
WS7/Comms Express Compilation problem
I am just doing an implementation of the JES5 comms suite on WS7 and am getting the following error after logging in. Two questions:
1) Could someone send me a working server.xml (with the right classpath)
2) Has anyone else seen this error.
Web server Version
[24/May/2007:10:46:20] info (25597): CORE1116: Sun Java System Web Server 7.0 B12/04/2006 10:15
[24/May/2007:10:46:21] info (25598): CORE5076: Using [Java HotSpot(TM) Server VM, Version 1.5.0_09] from [Sun Microsystems Inc.]
JVM version
/var/opt/SUNWwbsvr7/https-ce2.mail.uwo.pri/logs
[9:56am ce2]# /usr/jdk/latest/bin/javac -version
javac 1.5.0_09
Error message
Application Error
org.apache.jasper.JasperException: PWC6033: Unable to compile class for JSP
PWC6199: Generated servlet error:
/var/opt/SUNWwbsvr7/https-ce2.mail.uwo.pri/generated/ce2.mail.uwo.pri/uwc/org/apache/jsp/uwc/mailclient/mail_jsp.java:306: jspxmeth_jato_text_0(javax.servlet.jsp.tagext.JspTag,javax.servlet.jsp.PageContext) in org.apache.jsp.uwc.mailclient.mail_jsp cannot be applied to (com.iplanet.jato.taglib.UseViewBeanTag,javax.servlet.jsp.PageContext)
if (_jspx_meth_jato_text_0(_jspx_th_jato_useViewBean_0, jspxpage_context))
^
Note: /var/opt/SUNWwbsvr7/https-ce2.mail.uwo.pri/generated/ce2.mail.uwo.pri/uwc/org/apache/jsp/uwc/mailclient/mail_jsp.java uses unchecked or unsafe operations.
PWC6199: Generated servlet error:
Note: Recompile with -Xlint:unchecked for details.
1 errorI can send this to you offline if that makes it simpler.
From WS 7/JCS 5 installation on Solaris 10 Update 3 11/06
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2006 Sun Microsystems, Inc. All rights reserved.
Use is subject to license terms.
-->
<server>
<cluster>
<local-host>homer.cityofprescott.org</local-host>
<instance>
<host>homer.cityofprescott.org</host>
</instance>
</cluster>
<log>
<log-file>../logs/errors</log-file>
<log-level>info</log-level>
</log>
<temp-path>/tmp/https-homer.cityofprescott.org-2fe52e21</temp-path>
<user>webservd</user>
<jvm>
<java-home>/usr/jdk/entsys-j2se</java-home>
<server-class-path>/opt/SUNWwbsvr7/lib/webserv-rt.jar:/opt/SUNWwbsvr7/lib/pwc.jar:${java.home}/lib/tools.jar:/opt/SUNWwbsvr7/lib
/webserv-jstl.jar:/opt/SUNWwbsvr7/lib/container-auth.jar:/opt/SUNWwbsvr7/lib/jsf-impl.jar:/opt/SUNWwbsvr7/lib/jsf-api.jar:/usr/sfw/l
ib/ant/ant.jar:/usr/share/lib/ktsearch.jar:/opt/SUNWjax/share/lib/jaxws-api.jar:/opt/SUNWjax/share/lib/jaxws-rt.jar:/opt/SUNWjax/sha
re/lib/jaxws-tools.jar:/opt/SUNWjax/share/lib/jsr181-api.jar:/opt/SUNWjax/share/lib/jsr250-api.jar:/opt/SUNWjax/share/lib/jaxb-api.j
ar:/opt/SUNWjax/share/lib/jaxb-impl.jar:/opt/SUNWjax/share/lib/jaxb-xjc.jar:/opt/SUNWjax/share/lib/sjsxp.jar:/opt/SUNWjax/share/lib/
jsr173_api.jar:/opt/SUNWjax/share/lib/saaj-api.jar:/opt/SUNWjax/share/lib/saaj-impl.jar:/opt/SUNWjax/share/lib/xmldsig.jar:/opt/SUNW
jax/share/lib/xmlsec.jar:/opt/SUNWjax/share/lib/xws-security.jar:/opt/SUNWjax/share/lib/xws-security_jaxrpc.jar:/opt/SUNWjax/share/l
ib/wss-provider-update.jar:/opt/SUNWjax/share/lib/security-plugin.jar:/opt/SUNWjax/share/lib/FastInfoset.jar:/opt/SUNWjax/share/lib/
relaxngDatatype.jar:/opt/SUNWjax/share/lib/resolver.jar:/usr/share/lib/mail.jar:/usr/share/lib/activation.jar</server-class-path>
<class-path-suffix>:/opt/SUNWam/lib/xmlsec.jar:/usr/share/lib/endorsed/dom.jar:/opt/SUNWam/lib/saaj-api.jar:/opt/SUNWam/lib/jaxr
pc-api.jar:/opt/SUNWam/lib/jaxrpc-impl.jar:/opt/SUNWam/lib/jaxrpc-spi.jar:/opt/SUNWam/lib/saaj-impl.jar:/etc/opt/SUNWam/config:/opt/
SUNWam/lib:/opt/SUNWam/locale:/usr/share/lib/mps/secv1/jss4.jar:/opt/SUNWam/lib/am_sdk.jar:/usr/share/lib/ldapjdk.jar:/opt/SUNWam/li
b/am_services.jar:/opt/SUNWam/lib/am_sso_provider.jar:/opt/SUNWam/lib/swec.jar:/opt/SUNWam/lib/acmecrypt.jar:/opt/SUNWam/lib/iaik_ss
l.jar:/opt/SUNWam/lib/iaik_jce_full.jar:/opt/SUNWam/lib/mail.jar:/opt/SUNWam/lib/activation.jar:/opt/SUNWam/lib/am_logging.jar:/opt/
SUNWam/lib/jaas.jar:/opt/SUNWam/lib/jax-qname.jar:/opt/SUNWam/lib/namespace.jar:/opt/SUNWam/lib/relaxngDatatype.jar:/opt/SUNWam/lib/
xsdlib.jar:/opt/SUNWam/lib/jaxb-api.jar:/opt/SUNWam/lib/jaxb-impl.jar:/opt/SUNWam/lib/jaxb-libs.jar:/opt/SUNWam/lib/jaxb-xjc.jar:/op
t/SUNWam/lib/amclientsdk.jar:/opt/SUNWma/lib/wireless_rendering.jar:/opt/SUNWma/lib/wireless_rendering_util.jar:/opt/SUNWma/lib/mobi
le_services.jar:/opt/SUNWma/lib/ccpp-1_0.jar:/opt/SUNWma/lib/ccpp-ri-1_0.jar:/opt/SUNWma/lib/jena-1.4.0.jar:/opt/SUNWma/lib/rdffilte
r.jar:/opt/SUNWma/lib/locale:/opt/SUNWam/lib/mobile_identity.jar:/opt/SUNWmfwk/lib/mfwk_instrum_tk.jar:/opt/SUNWjdmk/5.1/lib/jdmkrt.
jar:/opt/SUNWjdmk/5.1/lib/jmxremote.jar:/opt/SUNWjdmk/5.1/lib/jmx.jar:/opt/SUNWcomm/lib/jars/commcli-client.jar:/opt/SUNWcomm/lib/ja
rs/jdapi.jar:/opt/SUNWcomm/lib/jars/commcli-callback.jar</class-path-suffix>
<debug>false</debug>
<debug-jvm-options>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7896</debug-jvm-options>
<jvm-options>-Djava.security.auth.login.config=login.conf</jvm-options>
<jvm-options>-Xms128m -Xmx256m</jvm-options>
<jvm-options>-Djava.protocol.handler.pkgs=com.iplanet.services.comm</jvm-options>
<jvm-options>-DLOG_COMPATMODE=Off</jvm-options>
<jvm-options>-Ds1is.java.util.logging.config.class=com.sun.identity.log.s1is.LogConfigReader</jvm-options>
<jvm-options>-Dcom.iplanet.am.serverMode=true</jvm-options>
</jvm>
<thread-pool>
<max-threads>128</max-threads>
<stack-size>131072</stack-size>
</thread-pool>
<default-auth-db-name>keyfile</default-auth-db-name>
<auth-db>
<name>keyfile</name>
<url>file</url>
<property>
<name>syntax</name>
<value>keyfile</value>
</property>
<property>
<name>keyfile</name>
<value>keyfile</value>
</property>
</auth-db>
<acl-file>default.acl</acl-file>
<mime-file>mime.types</mime-file>
<access-log>
<file>../logs/access</file>
</access-log>
<http-listener>
<name>http-listener-1</name>
<port>80</port>
<server-name>homer.cityofprescott.org</server-name>
<default-virtual-server-name>homer.cityofprescott.org</default-virtual-server-name>
</http-listener>
<virtual-server>
<name>homer.cityofprescott.org</name>
<host>homer.cityofprescott.org</host>
<http-listener-name>http-listener-1</http-listener-name>
<document-root>/var/opt/SUNWwbsvr7/https-homer.cityofprescott.org/docs</document-root>
<web-app>
<uri>/amserver</uri>
<path>../web-app/homer.cityofprescott.org/amserver</path>
</web-app>
<web-app>
<uri>/ampassword</uri>
<path>../web-app/homer.cityofprescott.org/ampassword</path>
</web-app>
<web-app>
<uri>/amcommon</uri>
<path>../web-app/homer.cityofprescott.org/amcommon</path>
</web-app>
<web-app>
<uri>/amconsole</uri>
<path>../web-app/homer.cityofprescott.org/amconsole</path>
</web-app>
<web-app>
<uri>/im</uri>
<path>../web-app/homer.cityofprescott.org/im</path>
</web-app>
<web-app>
<uri>/da</uri>
<path>../web-app/homer.cityofprescott.org/da</path>
</web-app>
<web-app>
<uri>/commcli</uri>
<path>../web-app/homer.cityofprescott.org/commcli</path>
</web-app>
<web-app>
<uri>/uwc</uri>
<path>/var/opt/SUNWuwc</path>
</web-app>
</virtual-server>
</server> -
Keystroke regular expression (regex) problem
I have a text field that should only permit the following values: "V", "X" or "NA" (all uppercase).
I'm fairly OK with javascript in pdf and I have a script that validates these when the field losses focus.
What would be much better is to have a keystroke script that only permits these values to be entered. I've found elsewhere in the forums a useful starting point here:
var re = /^[A-Za-z0-9 :\\_]$/;
event.change = event.change.toUpperCase();
if (event.change.length >0) {
if (event.willCommit == false) {
if (!re.test(event.change)) {
event.rc = false
but I've been unable to come up with anything for the regexp pattern that works reliably (these are definitely not my strong point).
Grateful thanks for any help.You can apply the RegEX to the keystroke and restrict the keystroke values that be entered or you can use the validation tab to enter a script to restrict the input to specific values.
The script you are using only checks the entered character so it will not test the string "NA" which is 2 characters.
Another approach for such a limited number of input options would be a combo box or drop down box where the options are space, "V", "X", and "NA". The value of the field would then be limited to a space (no selection made), a V, X, or NA. -
Problems with java regular expressions
Hi everybody,
Could someone please help me sort out an issue with Java regular expressions? I have been using regular expressions in Python for years and I cannot figure out how to do what I am trying to do in Java.
For example, I have this code in java:
import java.util.regex.*;
String text = "abc";
Pattern p = Pattern.compile("(a)b(c)");
Matcher m = p.matcher(text);
if (m.matches())
int count = m.groupCount();
System.out.println("Groups found " + String.valueOf(count) );
for (int i = 0; i < count; i++)
System.out.println("group " + String.valueOf(i) + " " + m.group(i));
My expectation is that group 0 would capture "abc", group 1 - "a" and group 2 - "c". Yet, I I get this:
Groups found 2
group 0 abc
group 1 a
I have tried other patterns and input text but the issue remains the same: no matter what, I cannot capture any paranthesized expression found in the pattern except for the first one. I tried the same example with Jakarta Regexp 1.5 and that works without any problems, I get what I expect.
I am using Java 1.5.0 on Mac OS X 10.4.
Thank to all who can help.paulcw wrote:
If the group count is X, then there are X plus one groups to go through: 0 for the whole match, then 1 through X for the individual groups.It does seem confusing that the designers chose to exclude the zero-group from group count, but the documentation is clear.
Matcher.groupCount():
Group zero denotes the entire pattern by convention. It is not included in this count. -
Regular expression and line break problem
Hi friends,
I was trying out one regular expression in java but, for some reasons its not working. The test string looks something like this in the eclipse watch window:-
Test string:-*
Content-Disposition: form-data; name="name"\n\nOmkar\n|
In the above test string the last character is a pipe symbol.
And the java code i have written is: -
code:
public static void getParameterValue(String paramName, String testParameter) {
String parameterRegex = "name=\""+paramName+"\"(.*)|";
Pattern pattern = Pattern.compile(parameterRegex,Pattern.DOTALL);
Matcher matcher = pattern.matcher(testParameter);
boolean isFound = matcher.find();
if (isFound) {
System.out.println("The parameter value is : "+matcher.group(1));
}After, the debug line moves over the first line in this method, the value contained in the "parameterRegex" string variable is:-
Regex:-*
name="name"(.*)|
However, my SOP statement, prints null....even though, "isFound" variable is true....can someone please explain me what could be the problem, where i am going wrong ?....
I have even, set the DOTALL option for the regex.
please some one help me!
Thanks and Regards
Omkar Patkarzomkar wrote:
Sorry, folks, i did not mention the fact that i had posted the my query, in Java ranch also. But my problem lead to another problem of CROSS - POSTING !
Apologies, for that !....but please, does any one have a solution to my regex ? .... any one ? please ?
The value i get in paramaname is :- name
which is present in double quotes in test string.
is my regex incorrect ?The discussion at JavaRanch seems to be pretty alive, so why not leave this thread and continue there? That way, people won't have to first read two different threads on two different forums before trying to help you. -
Problem with Regular Expression
Hi There!!
I have a problem with regular expression. I want to validate that one word and second word are same. For that I have written a regex
Pattern p=Pattern.compile("([a-z][a-zA-Z]*)\\s\1");
Matcher m=p.matcher("nikhil nikhil");
boolean t=m.matches();
if (t)
System.out.println("There is a match");
else
System.out.println("There is no match");
The result I am getting is always "There is no match
Your timely help will be much appreciated.
RegardsRam wrote:
ErasP wrote:
You are missing a backward slash in the regex
Pattern p = Pattern.compile("([a-z][a-zA-Z]*)\\s\\1");
But this will fail in this case.
Matcher m = p.matcher("Nikhil Nikhil");It is the reason for that *[a-z]*.The OP had [a-z][a-zA-Z]* in his code, so presumably he know what that means and wants that String not to match. -
Problem forming regular expression
Hi,
I want to use regular expression to match a repeating pattern until it meets a particular character. Eg.
The string I am trying to match is a function definition.
function abc()
This is the pattern I am using to match the above string.
Pattern p =
Pattern.compile("function abc.*\r*\n*\\{(\r*\n*.*)*(\r\n)*\\}", Pattern.MULTILINE);
"function abc" takes care of the method signature
" .*\r*\n*\\{" takes care of all spaces/characters/newline till it encounters the opnening brace
"(\r*\n*.*)*" this iswhere the problem lies. This matches all the individual new lines in the method body. But I dont want it to match the closing brace "}" . It is doing that currently. How can I avoid that? So that the next part of the pattern i.e. "\\}" actually matches the closing brace?DrLaszloJamf wrote:
Isn't the basic difference between regular grammars and context free grammars? The latter can generate matched parens, the former can't?I guess you mean by "regular grammar" a "regular language" (forgive my ignorance if they are the same)? But yes, AFAIK that is a difference. However, there are regex engines that can cope with these recursive matches (an undefined number of back references). Perl is one of them.
It is an often heard complaint about implementations of regular expression engines: they have grown too big for what they were created for. -
Problem with Regular Expressions
Hi Everyone:
I'm having a problem finding the easiest way to retrieve the replacement text that has been edited to insert back-references from previous matches. For instance,
Lets say I want to use the below regular expression
foo_bar([1-9])_fun
on the search text
foo_bar1_fun
foo_bar2_fun
foo_bar3_fun
and then the replacement text would be
foobar_fun$1
so in the end I would get
foobar_fun1
foobar_fun2
foobar_fun3
What I would like to do is be able to extract the replacement text that has been modified with the back reference matches after I use the Matcher.appendReplacement(stringbuffer, string) method.
So to clarify further, after I find the first match and use the appendReplacement Method, I would like to extract just the replacement that was made to the text, in this case foobar_fun1
Thanks for any help!Alright, thanks for the reply. I'll try and make this a little more clear
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LeClassName {
public static void main(String[] args) {
String input = "12341234foo_bar1_fun24342345foo_bar2_fun3522545foo_bar3_fun3423432";
Pattern pattern = Pattern.compile("foo_bar([1-9])_fun");
StringBuffer sb = new StringBuffer();
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
matcher.appendReplacement(sb, "foobar_fun$1");
//after the first pass through, I would like to extract, foobar_fun1
// but what is in sb is 12341234foobar_fun1
System.out.println(sb.toString());
}I did find a solution myself after a bit of thinking but if anyone can come up with a better solution do tell and I'll award that person the answer. Thanks again!
My Solution:
private String fixBackReplacements() throws IndexOutOfBoundsException {
String currentMatch = this.findMatch.group();
Pattern temporaryPattern = Pattern.compile(findText);
Matcher temporaryMatcher = temporaryPattern.matcher(currentMatch);
return temporaryMatcher.replaceFirst(replaceText);
} -
Urgent!!! Problem in regular expression for matching braces
Hi,
For the example below, can I write a regular expression to store getting key, value pairs.
example: ((abc def) (ghi jkl) (a ((b c) (d e))) (mno pqr) (a ((abc def))))
in the above example
abc is key & def is value
ghi is key & jkl is value
a is key & ((b c) (d e)) is value
and so on.
can anybody pls help me in resolving this problem using regular expressions...
Thanks in advance"((key1 value1) (key2 value2) (key3 ((key4 value4)
(key5 value5))) (key6 value6) (key7 ((key8 value8)
(key9 value9))))"
I want to write a regular expression in java to parse
the above string and store the result in hash table
as below
key1 value1
key2 value2
key3 ((key4 value4) (key5 value5))
key4 value4
key5 value5
key6 value6
key7 ((key8 value8) (key9 value9))
key8 value8
key9 value9
please let me know, if it is not possible with
regular expressions the effective way of solving itYes, it is possible with a recursive regular expression.
Unfortunately Java does not provide a recursive regular expression construct.
$_ = "((key1 value1) (key2 value2) (key3 ((key4 value4) (key5 value5))) (key6 value6) (key7 ((key8 value8) (key9 value9))))";
my $paren;
$paren = qr/
[^()]+ # Not parens
|
(??{ $paren }) # Another balanced group (not interpolated yet)
/x;
my $r = qr/^(.*?)\((\w+?) (\w+?|(??{$paren}))\)\s*(.*?)$/;
while ($_) {
match()
# operates on $_
sub match {
my @v;
@v = m/$r/;
if (defined $v[3]) {
$_ = $v[2];
while (/\(/) {
match();
print "\"",$v[1],"\" \"",$v[2],"\"";
$_ = $v[0].$v[3];
else { $_ = ""; }
C:\usr\schodtt\src\java\forum\n00b\regex>perl recurse.pl
"key1" "value1"
"key2" "value2"
"key4" "value4"
"key5" "value5"
"key3" "((key4 value4) (key5 value5))"
"key6" "value6"
"key8" "value8"
"key9" "value9"
"key7" "((key8 value8) (key9 value9))"
C:\usr\schodtt\src\java\forum\n00b\regex> -
Problem in creating a Regular Expression with gnu
Hi All,
iam trying to create a regular expression using gnu package api..
gnu.regex.RE;
i need to validate the browser's(MSIE) userAgent through my regular expression
userAgent is like :First one ==> Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
i wrote an regular expression like this:
Mozilla.*(.*)\\s*(.*)compatible;\\s*MSIE(.*)\\s*(.*)([0-9]\\.[0-9])(.*);\\s*(.*)Windows(.*)\\s*NT(.*)\\s*5.0(.*)
Actaully this is validating my userAgent and returns true, my problem is, it is returning true if userAgent is having more words at the end after Windows NT 5.0 like Second One ==> Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) Testing
i want the regularExpression pattern to validate the First one and return true for it, and has to return false for the Second one..
my code is:
import gnu.regexp.*;
import gnu.regexp.REException;
public class TestRegexp
public static boolean getUserAgentDetails(String userAgent)
boolean isvalid = false;
RE regexp = new RE("Mozilla.*(.*)\\s*(.*)compatible;\\s*MSIE(.*)\\s*(.*)([0-9]\\.[0-9])(.*);\\s*(.*)Windows(.*)\\s*NT(.*)\\s*5.0(.*)");
isvalid = regexp.isMatch(userAgent);
return isvalid;
public static void main(String a[])
String userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
boolean regoutput = getUserAgentDetails(userAgent);
System.out.println("***** regoutput is ****** " + regoutput);
}please help me in solving this..
Thanks in Advance..
thanx,
krishnaOfcourse, i can do comparision with simple string matching..
but problem is the userAgent that i want to support is for all the MSIE versions ranging from 5.0 onwards, so there will the version difference of IE like MSIE 6.0..! or MSIE 5.5 some thing like that..
any ways i will try with StringTokenizer once..!
seems that will do my work..
Thanks,
krishna -
Hi @ all,
I need a complicate regular expression, I don´t know.
I have a big folder with many .htm pages (800-1000) and I have to do the following:
http://www.domain.de/ab%32-xyz?myshop=123
I have to delete the "ab%32-xyz", the problem is, that in this are, there could be every symbol, letter or number.
So the area between "http://www.domain.de/" and "?myshop=123" (these 2 areas are everytime identical in all documents) shoud be deleted.
Could everyone say me, how to do this with regular expressions in dreamweaver?
Thanks,
Felix
P.S.: Sorry, my Engish is not so good, I´m from GermanyDo you want to replace the random text with anything?
If not, this is how you do it in DW:
Make a backup of the folder you want to edit, just in case anything goes wrong
Edit > Find and Replace
In the Find and Replace dialog box, select Folder from the "Find in" drop-down menu, and select the folder you want to work with.
Select Source Code from the Search drop-down menu.
Put the following code in the Find text area:
(http://www\.domain\.de/)[^?]+(\?myshop=123)
Put the following code in the Replace text area:
$1$2
In Options, select the "Use regular expression" check box.
Click Replace All. Dreamweaver will warn you that the operation cannot be undone in pages that aren't currently open. As long as you have made a backup, click OK to perform the operation.
Maybe you are looking for
-
Deskjet 9800 doesn't recognize new color cartridge
When my Deskjet 9800 printer reported that the color cartrridge was out of ink and I replaced it several months ago, I put in a replacement cartridge (#97) from a new package that I had in reserve. The printer failed to recognize it, continuing to r
-
Where is my music????
I've just bought an external hard drive, as with 17GB of music in iTunes my poor little eMac was really struggling. My son has transferred the iTunes folder to the new hard drive and when you click on the new folder and go to 'get info', sure enough
-
Need some modelinng help...
Hello, I have model as shown below SF3 joining M:1 on D1 and SF1, SF2 joining 1:M on D1, which in turn joins 1:M on F1 and F2. I have put aggregations on Measure1 and Measure2 for F1 and F2, so that when user pulls columns from all the dimensions the
-
we have reprocess order type as ZP12, When a reprocess orde to be done, what we do we create a order original order 13899034 Reprocess order as 13899034QAA based on the reprocess type. But the issue the original order has the sales order link. But th
-
Offset problem more than 240 characters
hi all, please find the coding,when i run this program i got dump. how to correct this dump. wa_prchr = 'In Stahl India various sales scenarios are created standard orders (Order type - OR)Sale order is created based on the customer purchase order