String.matches() question - regular expression help
How come the following code's if condition returns false?
String someFile="Dr. Phil.pdf";
if (someFile.matches("[.][Pp][Dd][Ff]$")) {
System.out.println("File is a pdf file.");
}When I change the the matches method to matches(".*[Pp][Dd][Ff]$") it works, so does that mean it has to match the entire string to return true? If so, how can I determine if a partial match occured?
If partial matching isn't feasible, then can someone help me look determine if this is the best matching pattern to use:
matches(".*[.][Pp][Dd][Ff]$")Thanks.
The documentation is your friend.
[String.matches(regex)|http://java.sun.com/javase/6/docs/api/java/lang/String.html#matches(java.lang.String)] says:
An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression
Pattern.matches(regex, str)And [Pattern.matches(regex, str)|http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#matches(java.lang.String, java.lang.CharSequence)] says
behaves in exactly the same way as the expression
Pattern.compile(regex).matcher(input).matches()And [Matcher.matches()|http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html#matches()] says
Attempts to match the entire region against the pattern.
Similar Messages
-
Matches from regular expression into collection
Hello,
I need to do the following:
I have a long string with some similar repeated data. I would like, using a regular expression, to extracts all matches in a collection. Is there a way of performing this task?
I have look through the owa_pattern package, but as far as I found out, I can extract only a simple match. Here is an exact quote:
"If multiple overlapping strings can match the regular expression, this function takes the longest match. " - http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/w_patt.htm
So what can I do if I want to get all the matches?
Thank you in anticipation. Any help would be appreciated.
Best regards,
beroetzI think your need a tokenizer-function.
If the string +:in_str+ is delimited by +:in_delimiter+ you could try this:
SELECT REGEXP_REPLACE(REGEXP_SUBSTR( :in_str || :in_delimiter, '(.*?)' || :in_delimiter, 1, LEVEL ), :in_delimiter, '') TOKEN
BULK COLLECT INTO :my_nested_table
FROM DUAL
CONNECT BY REGEXP_INSTR( :in_str || :in_delimiter, '(.*?)' || :in_delimiter, 1, LEVEL ) > 0
ORDER BY LEVEL ASC;
I wrote a string-to-textarray-tokenizer (and it's pendant) some times ago, being able to cut from certain positions within the string using regular expressions and return the elements into an nested table of varchar2. It looks like:
TYPE pos_arraytype IS TABLE OF POSITIVE ;
TYPE text_arraytype IS TABLE OF VARCHAR2(2000);
FUNCTION stringToTextarray(in_str IN VARCHAR2, in_pos_arr IN pos_arraytype, in_regexp_arr IN text_arraytype DEFAULT NULL, in_trim_strings IN BOOLEAN DEFAULT TRUE)
RETURN text_arraytype ;
in_str is the string to be tokenized
in_pos_arr is a table of positive values of positions in the string to be cut
in_regexp_arr is a table of regular expressions to use at each position declared by in_pos_arr
in_trim_strings is a flag, if the cutted element should be trimmed
using above for example:
in_str = 'Markus van Muster 347651234XY Musterdaam ABCDE'
in_pos_arr = (1, 13, 35, 35, 42)
in_regexp_arr = ('(.?){12}', '([^[:digit:]]?){22}', '[[:digit:]]{4}', '[[:alpha:]]{2}', '(.?){14}')
in_trim_strings = TRUE
RETURN collection ('Markus','van Muster','1234','XY','Musterdaam')
If you need the code, then tell me! I'm looking for....
Cheers,
Martin
Edited by: Nuerni on 17.10.2008 08:49 -
Converting String Characters into Regular Expression Automatically?
Hi guys.... is there any program or sample coding which is available to convert string characters into regular expression automatically when the program is run?
Example:
String Character Input: fnffffffffffnnnnnnnnnffffffnnfnnnnnnnnnfnnfnfnfffnfnfnfnfnfnnnnd
When the program runs, it automatically convert into this :
Regular Expression Output: f*dhey guys.... i am sorry for not providing all the information that you guys need as i was rushing off to urgent meeting... for my string characters i only have a to n.. all these characters are collected from sensors and stored inside database... from many demos i have done... i found out that every demo has different strings of characters collected and these string of characters will not match with the regular expressions that i had created due to several unwanted inputs and stuff... i have a lot of different types of plan activities and therefore a lot of regular expressions.... if i put [a-z|0-9]*... it will capture all characters but in the same time it will be showing 1 plan only.... therefore, i am finding ways to get the strings i collected and let it form into regular expression by themselves in the program so that it will appear as different plans as output with comparing with the regular expression that i had created.... is there any way to do so?
please post again if there is any questions u are still not familiar with... thank you... -
XML node name matching with regular expressions
Hello,
If i have an xml file that has the following:
<parameter>
<name>M2-WIDTH</name>
<value column="09" date="2004-10-31T19:56:30" row="03" waferID="PUK444150-20">10.4518</value>
</parameter>
<parameter>
<name>M2-GAP</name>
<value column="29" date="2004-10-31T19:56:30" row="06" waferID="PUK444150-03">2.864</value>
</parameter>
<parameter>
<name>RES-LENGTH</name>
<value column="29" date="2004-10-31T19:56:30" row="06" waferID="PUK444150-03">2.864</value>
</parameter>
Is there anyway i can get a list of nodes that match a certain pattern say where name=M2* ?
I cant seem to find any information where i can match a regular expression. I see how you can do:
String expression=/parameter[@name='M2-LENG']/value/text()";
NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource, XPathConstants.NODESET);
But i want to be able to say:
String expression=/parameter[@name='M2-*']/value/text()";
Is this possible? if so how can i do this?
Thanks!As implemented in Java, XPath does not support regular expressions, but in most cases there are workarounds thanks to XPath functions. Correct me if I'm wrong, but setting your expression against the XML document (i.e. because there are no "name" attributes in the whole document) I think you mean to get the value of the <value> elements that have a <parameter> parent element and a <name> sibling element whose value starts with "M2-". If that is the case, you can use the following query expression:String expression = "//parameter/value[substring(../name,1,3)='M2-']";Sorry if I misunderstood the meaning of your expression, but I hope this will help you get the hang of using XPath functions as a substitute for regular expressions.
-
Pattern matching using Regular expression
Hi,
I am working on pattern matching using regular expression. I the table, I have 2 columns A and B
A has value 'A499BPAU4A32A386KBCZ4C13C41D20E'
B has value like '*CZ4*M11*7NQ+RDR+RSM-R9A-R9B'
the requirement is that I have to match the columns of B in A. If there is a value with * sign, this must be present in A like 'CZ4' should exit in string A.
The issue I am facing is that there are 2 values with * sign. The code works fine for first match (CZ4) but it does not look further as M11 does not exist in A.
I used the condition
AND instr(A,substr(REGEXP_SUBSTR(B, '*[^*]{3}'),2) ,1)=0
First of all, is this possible to match multiple patterns in one condition?
If yes, please suggest.
Thanksuser2544469 wrote:
Thanks a lot Frank. This query worked wonderful for the test data I have provided however I have some concerns:
- query doesnot include the column BOOK which is a mandatory check.Sorry, that was my mistake. It was a very easy mistake to make, since you posted sample data where it didn't matter. Instead of doing a cross-join between vn and got_must_have_cnt, do an inner join, using book. That means book will have to be in got_must_have_cnt, and all the sub-queries from which it descends. Look for comments that say "March 22".
If you want to treat '+' in test_cat.codes as '*', then the simplest thing is probably just to use REPLACE, so that when the table has '+', you use '*' instead.
WITH got_token_cnt AS
SELECT cat
, book -- Added March 22
, REPLACE (codes, '+', '*') AS codes -- If desired. Changed March 22
, LENGTH (codes) - LENGTH ( TRANSLATE ( codes
, 'x*+-'
, 'x'
) AS token_cnt
FROM test_cat
, cntr AS
SELECT LEVEL AS n
FROM ( SELECT MAX (token_cnt) AS max_token_cnt
FROM got_token_cnt
CONNECT BY LEVEL <= max_token_cnt
, got_tokens AS
SELECT t.cat
, t.book -- Added March 22
, REGEXP_SUBSTR ( t.codes
, '[*+-]'
, 1
, c.n
) AS token_type
, SUBSTR ( REGEXP_SUBSTR ( t.codes
, '[*+-][^*+-]*'
, 1
, c.n
, 2
) AS token
FROM got_token_cnt t
JOIN cntr c ON c.n <= t.token_cnt
, got_must_have_cnt AS
SELECT cat, book -- Changed March 22
, COUNT (CASE WHEN token_type = '*' THEN 1 END) AS must_have_cnt
FROM got_tokens
GROUP BY cat, book -- Changed March 22
SELECT mh.cat
, vn.vn_no
FROM got_must_have_cnt mh
JOIN vn ON mh.book = vn.book -- Changed March 22
LEFT OUTER JOIN got_tokens gt ON mh.cat = gt.cat
AND INSTR (vn.codes, gt.token) > 1
GROUP BY mh.cat
, mh.must_have_cnt
, vn.vn_no
HAVING COUNT (CASE WHEN gt.token_type = '*' THEN 1 END) = mh.must_have_cnt
AND COUNT (CASE WHEN gt.token_type = '-' THEN 1 END) = 0
ORDER BY mh.cat
- query is very slow with 60000 records in vn table. Cost is somewhere around 36000.See these threads:
When your query takes too long ...
HOW TO: Post a SQL statement tuning request - template posting
Relational databases were designed to have (at most) one piece of information in each column. If you decide to have multiple items in the same column (as you have a variable number of tokens in the codes column), don't be surprised if that makes things slower and more complicated. Most of the query I posted, and perhaps most of the time needed, is jsut to normalize the data. If you stored the data in a narmalized form, perhaps something like got_tokens, then you wouldn't need the first 3 sub-queries that I posted.
Edited by: Frank Kulash on Mar 22, 2011 12:04 PM -
How to create a list of string if a regular expression is given ?
Hi folks,
I have a regular expression say abcd[a-z]\\\.[0-9] . ( please ignore one '\')
For this string i know that
following string matches successfully
1. abca.0
2. abcb.1
3. abcz.9 ......etc n number of combination are possible.
is there any algorithm which will create some randomn strings from a regular expression.
input to algorithm : some string pattern
output to algorithm : some matching strings ( can be a single or an array of matching strings)
Thanks in advance..
Sethu
Edited by: Sethumadhavan on Apr 16, 2008 6:32 AMCan u please give little more explanation...
If i get some some values i can exit with the values ... and from the values i got i can ignore the duplicates ...
But i am not getting the basic algorithm to get list of strings.....( DFA? or NFA?)
thanks
sethu -
I haven't used regular expressions before, and I'm having trouble finding a regular expression to extract a string subset between two markers.
The string;
Header stuff I don't want
Header stuff I don't want
Header stuff I don't want
Header stuff I don't want
Header stuff I don't want
Header stuff I don't want
ERRORS 6
Info I want line 1
Info I want line 2
Info I want line 3
Info I want line 4
Info I want line 5
Info I want line 6
END_ERRORS
From the string above (this is read from a text file) I'm trying to extract the string subset between ERRORS 6 and END_ERRORS. The number of errors (6 in this case) can be any number 1 through 32, and the number of lines I want to extract will correspond with this number. I can supply this number from a calling VI if necessary.
My current solution, which works but is not very elegant;
(1) uses Match Regular Expression to the return the string after matching ERRORS 6
(2) uses Match Regular Expression returning all characters before match END_ERRORS of the string returned by (1)
Is there a way this can be accomplished using 1 Match Regular Expression? If so could anyone suggest how, together with an explanation of how the regular expression given works.
Many thanks
Alan
Solved!
Go to Solution.I used a character class to catch any word or whitespace characters. Putting this inside parentheses makes a submatch that you can get by expanding the Match Regular Expression node. The \d finds digits and the two *s repeat the previous term. So, \d* will find the '6', as well as '123456'.
Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice -
REGEX: question about finding Overlapping matches using regular expressions
I have the following problem.
Say for my pattern I use:
Pattern pattern = Pattern.compile("AAA");
Matcher matcher = pattern.matcher("AAAAAA");when I run a loop
while (matcher.find())
System.out.println("Match Found: "+matcher.start()+" "+matcher.end());I get 2 Hits shown in the following output:
Match Found: 0 3
Match Found: 3 6
therefore the regex is seeing the first AAA then the second AAA.
I want it to find the other AAA's in there that are overlapping the other two finds i.e. I want the output to find
AAA from 0 to 3
AAA from 1 to 4
AAA from 2 to 5 and finally
AAA from 3 to 6
thereby including the overlapping finds.
How can I do this using regex? what am I missing that prevents the overlapping matches to be found? Do I need a quantifier?
Thanks for the help!While the solutions above work fine with the given input, they don't really find all overlapping matches. They just find the longest possible match at each start position. Here's a more thorough approach:import java.util.*;
import java.util.regex.*;
public class Test
public static List<String> matchAllWays(String rgx, String str)
Pattern p = Pattern.compile(rgx);
Matcher m = p.matcher(str);
List<String> result = new ArrayList<String>();
int len = str.length();
int start = 0;
int end = len;
while (start < len && m.region(start, len).find())
start = m.start();
do
result.add(m.group());
end = m.end() - 1;
} while (end > start && m.region(start, end).find());
start++;
return result;
public static void main(String[] args)
List<String> matches = matchAllWays("a.*a", "abracadabra");
System.out.println(matches);
}This approach requires JDK 1.5 or later; that's when the regions API was added to Matcher. -
I need help writting a regular expression that will match the following strings:
1+1,1+(1+1),((1+1)+(1+1)),1+(1+(1+1)). Basically one that will match an arithmetic expression (operands limited to 1's and operators to '+' sign) with or without correctly matched parentheses. Have'nt had much luck so far. Any input will help. thanksokay, you asked for it:
[1+()]+
it will only match those string but it will tell you nothing about syntactically correct expressions. This is because these types of expression are not "regular" and cannot be properly parsed using regular expressions. -
Pattern and Matcher of Regular Expressions
Hello All,
MTMISRVLGLIRDQAISTTFGANAVTDAFWVAFRIPNFLRRLFAEGSFATAFVPVFTEVK
ETRPHADLRELMARVSGTLGGMLLLITALGLIFTPQLAAVFSDGAATNPEKYGLLVDLLR
LTFPFLLFVSLTALAGGALNSFQRFAIPALTPVILNLCMIAGALWLAPRLEVPILALGWA
VLVAGALQLLFQLPALKGIDLLTLPRWGWNHPDVRKVLTLMIPTLFGSSIAQINLMLDTV
IAARLADGSQSWLSLADRFLELPLGVFGVALGTVILPALARHHVKTDRSAFSGALDWGFR
TTLLIAMPAMLGLLLLAEPLVATLFQYRQFTAFDTRMTAMSVYGLSFGLPAYAMLKVLLP
I need some help with the regular expressions in java.
I have encountered a problem on how to retrieve two strings with Pattern and Matcher.
I have written this code to match one substring"MTMISRVLGLIRDQ", but I want to match multiple substrings in a string.
Pattern findstring = Pattern.compile("MTMISRVLGLIRDQ");
Matcher m = findstring.matcher(S);
while (m.find())
outputStream.println("Selected Sequence \"" + m.group() +
"\" starting at index " + m.start() +
" and ending at index " m.end() ".");
Any help would be appreciated.Double post: http://forum.java.sun.com/thread.jspa?threadID=726158&tstart=0
-
I'm using a RegExp class (http://www.jurjans.lv/flash/RegExp.html) to do some regular expression in AS2. But I'm not very good at it.
var str:String="What if there are other variables, such as possible <a class='gloss' href='asfunction:_root.handle, confounding variables'><b>confounding variables</b></a> which could explain at least some of the relationship between the two variables? Here <a href='' target='_blank'>is another link</a>.\n<a class='gloss' href='asfunction:_root.handle, confounding variables'>confounded variables</a>"
var reg1:RegExp = new RegExp("<a.*gloss.*href=[\'\"]?([^\\\'\">]+)>+(.*</a>)", "ig");
var obj:Object = reg1.exec(str);
while (obj != null) {
for(var a in obj){
if(!isNaN(a)){
trace(a+": "+obj[a]);
trace(newline);
obj = reg1.exec(str);
And this traces:
2: <b>confounding variables</b></a>
1: asfunction:_root.handle, confounding variables'
0: <a class='gloss' href='asfunction:_root.handle, confounding variables'><b>confounding variables</b></a>
2: confounded variables</a>
1: asfunction:_root.handle, confounding variables'
0: <a class='gloss' href='asfunction:_root.handle, confounding variables'>confounded variables</a>
I'm trying to get the href and the "friendly link" part of the anchor tag (but only for anchors that have a class of gloss).
As you can see I'm almost there, but I'm getting the extra </a> and the extra ' on the two examples. I tried putting the ) before the </a> but that just broke it. (Of course that could be because this class doesn't work properly, but I'm guessing that isn't the case.)
Anybody really good with regular expressions who can help me out?Looks like there is a "greedy" bug with the () in that AS2 implementation.
I also have a problem the expression matching not the next occurance of the closing </a> but the final one.
Anybody have any ideas of other ways to do this? -
String splitting with regular expressions
Hello everyone
I need some help in splitting the string using regular expressions
Suppose my String is : abc def "ghi jkl mno" pqr stu
after splitting the reulsting string array should contain the elements
abc
def
ghi jkl mno
pqr
stu
what my regular expression should beSince this is essentially the same as parsing CSV data, you might want to download a CSV parser and adapt it to your need. But if you want to use regexes, split() is not the way to go. This approach should work for your sample data:
Pattern p = Pattern.compile("\"[^\"]*+\"|\\S+");
Matcher m = p.matcher(input);
while (m.find())
System.out.println(m.group());
} -
Phone number Regular expression Help
Hi,
I am trying to validate phone number using regular expression.
The format shoud be either of the two given below. It should not
accept phone number of any other format other than the once given below.
I tried out the following regular expression
pn.matches("[\p{+}][0-9]+ ")
but it accepts alphabets too(which is wrong).
How do i check if the phone number is of the following format(OR condition).
0401234567
+358501234567
Any help would be kindly appriciated.
Thanks
SanamThere will probably be much more constraints on you phone numbers, but here's a start:String[] numbers = {"0401234567", // should be ok
"040123456", // wrong, one number too little
"+358501234567", // should be ok
"+3585012345670", // wrong, one number too much
"+35850123456"}; // wrong, one number too little
String regex = "\\+[0-9]{12}"+ // a + sign, followed by 12 numbers
"|"+ // OR
"0[0-9]{9}"; // a zero, followed by 9 numbers
for(String n : numbers) {
System.out.println("Is "+n+" valid? "+n.matches(regex));
} -
String replace using regular expressions
I'm not very good at regular expressions, but I would like my script to replace
<a href="somepage.html">
by
<a href="event:somepage">
How do I do this? Thanks in advance!Replacing a string that matches a certain pattern with another string is one of the more common RegEx tasks. There is documentation on using them here:
http://livedocs.adobe.com/flex/3/html/help.html?content=12_Using_Regular_Expressions_01.ht ml
hth,
matt horn
flex docs -
Regular Expression Help Please?
Hi
I'm trying to get my head round regular expressions in find
and replace,
it's a slow process for me!
I have this -
<a
href="
http://www.forms.mydomainname.com/cgi-bin/urltracker/tracker.pl?site=http://www.website-ad dress.com"
and I'm trying to change it to this -
<a
href="
http://www.forms.mydomainname.com/cgi-bin/urltracker/tracker.pl?site=http://www.website-ad dress.com&email="
I was trying first of all with a *.*, but couldn't work out
how to tell it
where the code ends?
They are hundred of pages like this, all with different
website-addresses.
After I have changed all the pages to the new code, I then
will need to copy
and paste an different email address to the end of each line,
to each page.
Unless anyone knows a way of automating that?
Hope someone can point me in the right direction?
Many thanks, Craig.Hi David
Many thanks for all that and the detailed descriptions.
I will be working through it all again tomorrow, so will put
your info to
the test! lol
As for partially building the email addresses, I think that
would be too
much,
as the emails are all over the place, some have their own
domain, other use
hotmail, Yahoo etc.
Some even have they own domain for their website and a free
one for the
email address.
They are all Hotels, B&B' & Cottages etc.
Hopefully all your hard work will help me a step closer to
understanding it
all.
Many thanks again,
Craig.
"David Stiller" <[email protected]> wrote in
message
news:[email protected]...
> Craig,
>
>> You do have that correct David, thanks.
>
> Okay.
Regex is as much an "exact science" as it is an "art
> form" -- which isn't to say I'm a regex artist; I just
love the
> technology -- but I mention this because I made the
following assumption
> in order to keep the pattern relatively simple: your
href values are all
> quoted in either single or double quotes. Such as, for
example, the
> following sample HTML ...
>
> <body>
> <a
> href="
http://www.forms.mydomainname.com/cgi-bin/urltracker/tracker.pl?site=www.sample.com">asfd< /a>
> <a
> href='
http://www.forms.mydomainname.com/cgi-bin/urltracker/tracker.pl?site=www.example.net'></a>
> <a
> href="
http://www.forms.mydomainname.com/cgi-bin/urltracker/tracker.pl?site=www.company.com"></a>
> </body>
>
> In the Find field, enter this pattern ...
>
> (tracker\.pl\?site=.*?)(["'])
>
> ... and in the Replace field, enter this pattern ...
>
> $1&email=ADDRESS$2
>
> Then carefully use your Find Next and Replace buttons to
step through
> your code. The above will add &email=ADDRESS to your
HTML in all the
> right places. I chose that because ADDRESS is easy to
select by double
> clicking, which should facilitate your replacing it.
>
>
> Let's step through the patterns.
>
> (tracker\.pl\?site=[^"']*?)(["'])
>
> This looks for the phrase "tracker.pl?site=" (without
quotes) followed
> immediately by a "non-greedy" match of any character
that isn't a single
> or double quotation mark, followed immediately by either
a single or
> double quotation mark. I took , which I took to be a
safe, short "hook"
> into the string we need. I split this pattern into two
sections, grouped
> by parentheses. This allows us to refer to the first
part of the match
> (everything but the closing quotation mark) as group 1,
and the second
> part (the closing quotatin mark) as group 2. This is
like storying values
> with your calculator's M (memory) button.
>
> $1&email=ADDRESS$2
>
> Here, we refer to group 1 and follow it with the phrase
> "&email=ADDRESS" (without quotes), followed again by
group 2.
>
> Now, in theory, we could use the domain name of each
unique site to at
> least partially build the email address. That would get
you even closer
> to your goal. To do so, I'd need even more detail from
you, such as the
> kinds of domains you have (how many sub domains are
probable, etc.).
>
>
> David
> stiller (at) quip (dot) net
> Dev essays:
http://www.quip.net/blog/
> "Luck is the residue of good design."
>
Maybe you are looking for
-
How can I set up my shaw email account to work on my iphone?
How can I set up my Shaw email account to work on my iphone? It doesn't want to work no matter what I try. Shaw's help line hasn't been very helpful so far.
-
How to, add new lines under 'Phrase' in keyboard shortcuts
Hi, new owner of a 4s and need to be able to add several lines such as an address to the 'phrase' box in keyboard shortcuts. If i hit return it takes me to the 'Shortcut' box. I need to create many of these shortcuts so using signature is not going
-
Hey everyone... I am having some trouble with passing values to and from a pop-up window. When running the application the user must select a value from a select list (which appears in every row of a query report), then another page must pop-up displ
-
Viewing Images from Database using JSP/ SQLJ
Dear Friends, I could manage to write an image into a BLOB Field in the Oracle Database. I could even manage to read that image from the database but i am doing it by reading the BLOB data and writing it into a file with 'gif' or 'jpeg' extension. I
-
Hi All When am creating service enrty sheet in ML81N am getting the following error. ""147 Account determination for entry CAIN WRX not possible"" With Rgds Chandra