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>
Similar Messages
-
How to form a regular expression for matching the xml tag?
hi i wanted to find the and match the xml tag for that i required to write the regex.
for exmple i have a string[] str={"<data>abc</data>"};
i want this string has to be splitted like this <data>, abc and </data>. so that i can read the splitted string value.
the above is for a small excercise but the tagname and value can be of combination of chars/digits/spl symbols like wise.
so please help me to write the regular expression for the above requirementyour suggestion is most appreciable if u can give the startup like how to do this. which parser is to be used and stuff like that
-
Regular Expression for Match Pattern (string) Function
I need to find a variable length string enclosed by brackets and
within a string. Can't seem to get the regular expression right for
the Match Pattern function. I'm able to get the job done using the
Token function, but it's not as slick or tight as I'd like. Does
anybody out there have the expression for this?Jean-Pierre Drolet wrote in message news:<[email protected]>...
> The regular expression is "\[[~\]]*\]" which means:
> look for a bracket "\[" (\ is the escape char)
> followed by a string not containing a closing bracket "[~\]]*"
> followed by a closing bracket "\]". The match string include the
> brackets
>
> You can also read "Scan from String" with the following format:
> "%[^\[]\[%[^\[\]]" and read the 2nd output. The brackets are removed
> from the scanned string.
Thanks, Jean_Pierre
I did some more experimenting after posting and found that \[.*\] also
works with the match pattern function. Thanks for your input.
sm -
Regular Expression for match pattern
Hi guys, I need some help.
In one part of my test system, I give to the program a sequence of temperatures, which are numbers separated by a comma. Due to a possible error, the user can forget the coma, and the program gets unexpected values.
For example, "20, 70, -10" is a right value, whereas "20 70, -10" would be a wrong one.
Which regular expression will detect this comma absence?
Thanx in advance.OK, makes it more difficult (and more fun )
My original version failed also when the space was forgotten. DOH!
Try this version. It get's complicated cause the scan from string likes ignoring spaces... So we change the spaces
Hope this helps
Shane.
PS The ideas given by others are still a better solution, but if you're stuck........
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
Attachments:
Check input string with commas(array).vi 39 KB -
Regular expressions for matching file path
Could someone give me idea that how can i compare a fixed path, with the paths user gives using regular expressions?
My fixed path is : src\com\sample\demo\work\gui\.**
and user may give like src\com\sample\demo\work\gui\test.jsp, src\com\sample\demo\work\gui\init.jsp etc.
Any ideas are appreciated and thanks in advance....and if you insist on using regexes, you'll have to double-escape the backslashes: if ( userString.matches("src\\\\com\\\\sample\\\\demo\\\\work\\\\gui\\\\.*") ) { Whether you use regexes or not, you'll save yourself a lot of hassle by converting all backslashes to forward slashes before you do anything with the strings: userString = userString.replace('\\', '/');
if ( userString.matches("src/com/sample/demo/work/gui/.*") ) {
// or...
if ( userString.startsWith("src/com/sample/demo/work/gui/") ) { -
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 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. -
Using regular expressions for validation in i18n
Can we use regular expressions for validation of inputs in a java application taking care of i18N aspects too. Zip code for different locales are different. Can we use regular expressions to validate zipcode inputs from different locales
hi,
For that shall i have to create individual patterns for matching the inputs from different locales or a single pattern will do in the case of validating phone nos. around the world, zip codes etc. In case different patterns are required, programmer should have a konwledge of difference in patters for different locales.
regards
sdas -
Regular Expression for PathName???
Anyone have a "ready to go" regular expression for detecting a pathname?
for example I need to detect the following:
myfile.txt
./myfile.txt
../my-file.ini
/home/my-home/myFile.foo
etc.
Now, in a perfect world, it could also do Windows (or ANY OS for that matter) pathnames (though this is not terrbibly important for my case at least).
TIA,
/mimport java.util.regex.*;
* @author Ian Schneider
public class FileRegex {
static Pattern pattern;
/** Creates a new instance of FileRegex */
public FileRegex() {
public Pattern getPattern() {
if (pattern == null) {
pattern = Pattern.compile("([\\/]?(\\w+|\\.|\\.\\.)[\\/])*(\\w+)\\.?(\\w+)?");
return pattern;
public String[] parts(String path) {
Matcher m = getPattern().matcher(path);
if (m.find()) {
return new String[] { m.group(1),m.group(3),m.group(4) };
return null;
public boolean matches(String path) {
return getPattern().matcher(path).matches();
public static final void main(String[] args) throws Exception {
FileRegex regex = new FileRegex();
String[] files = {
"myfile.txt",
"../myfile.txt",
"./myfile.txt",
"/a/b/c/myfile.txt",
"/a/../myfile.txt",
"myfile"
for (int i = 0, ii = files.length; i < ii; i++) {
System.out.println( files[i] + " match " + regex.matches(files));
String[] pieces = regex.parts(files[i]);
if (pieces != null)
System.out.println(" path : " + pieces[0] + " file : " + pieces[1] + " ext : " + pieces[2]);
I will leave it to you as an excercise to add support for spaces in path names, different separator characters, etc.. -
Regular Expression for IPAddress
Hello members.....
I am a new member to this forum
I am in need of the Regular Expression for IPAddress...
"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"....is the expression i wrote..But it is taking 0.0.0.0 as a valid IPAddress. (0.0.0.0 is not a valid IPAddress)
Please reply....awaiting
RajeshwarI am in need of the Regular Expression for
IPAddress...
"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"
....is the expression i wrote..But it is taking
0.0.0.0 as a valid IPAddress. (0.0.0.0 is not a valid
IPAddress)Your regex matches "999.999.999.999", which (of course) isn't a vaild IP address as well.
This one is closer, but still allows 0.0.0.0:
\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\bBut why not roll your own method which checks an IP address? -
"Match Regular Expression" and "Match Pattern" vi's behave differently
Hi,
I have a simple string matching need and by experimenting found that the "Match Regular Expression" and "Match Pattern" vi's behave somewhat differently. I'd assume that the regular expression inputs on both would behave the same. A difference I've discovered is that the "|" character (the "vertical bar" character, commonly used as an "or" operator) is recognized as such in the Match Regular Expression vi, but not in the Match Pattern vi (where it is taken literally). Furthermore, I cannot find any documentation in Help (on-line or in LabVIEW) about the "|" character usage in regular expressions. Is this documented anywhere?
For example, suppose I want to match any of the following 4 words: "The" or "quick" or "brown" or "fox". The regular expression "The|quick|brown|fox" (without the quotes) works for the Match Regular Expression vi but not the Match Pattern vi. Below is a picture of the block diagram and the front panel results:
The Help says that the Match Regular Expression vi performs somewhat slower than the Match Pattern vi, so I started with the latter. But since it doesn't work for me, I'll use the former. But does anyone have any idea of the speed difference? I'd assume it is negligible in such a simple example.
Thanks!
Solved!
Go to Solution.Yep-
You hit a point that's frustrated me a time or two as well (and incidentally, caused some hair-pulling that I can ill afford)
The hint is in the help file:
for Match regular expression "The Match Regular Expression function gives you more options for matching
strings but performs more slowly than the Match Pattern function....Use regular
expressions in this function to refine searches....
Characters to Find
Regular Expression
VOLTS
VOLTS
A plus sign or a minus sign
[+-]
A sequence of one or more digits
[0-9]+
Zero or more spaces
\s* or * (that is, a space followed by an asterisk)
One or more spaces, tabs, new lines, or carriage returns
[\t \r \n \s]+
One or more characters other than digits
[^0-9]+
The word Level only if it
appears at the beginning of the string
^Level
The word Volts only if it
appears at the end of the string
Volts$
The longest string within parentheses
The first string within parentheses but not containing any
parentheses within it
\([^()]*\)
A left bracket
A right bracket
cat, cag, cot, cog, dat, dag, dot, and dag
[cd][ao][tg]
cat or dog
cat|dog
dog, cat
dog, cat cat dog,cat
cat cat dog, and so on
((cat )*dog)
One or more of the letter a
followed by a space and the same number of the letter a, that is, a a, aa aa, aaa aaa, and so
on
(a+) \1
For Match Pattern "This function is similar to the Search and Replace
Pattern VI. The Match Pattern function gives you fewer options for matching
strings but performs more quickly than the Match Regular Expression
function. For example, the Match Pattern function does not support the
parenthesis or vertical bar (|) characters.
Characters to Find
Regular Expression
VOLTS
VOLTS
All uppercase and lowercase versions of volts, that is, VOLTS, Volts, volts, and so on
[Vv][Oo][Ll][Tt][Ss]
A space, a plus sign, or a minus sign
[+-]
A sequence of one or more digits
[0-9]+
Zero or more spaces
\s* or * (that is, a space followed by an asterisk)
One or more spaces, tabs, new lines, or carriage returns
[\t \r \n \s]+
One or more characters other than digits
[~0-9]+
The word Level only if it begins
at the offset position in the string
^Level
The word Volts only if it
appears at the end of the string
Volts$
The longest string within parentheses
The longest string within parentheses but not containing any
parentheses within it
([~()]*)
A left bracket
A right bracket
cat, dog, cot, dot, cog, and so on.
[cd][ao][tg]
Frustrating- but still managable.
Jeff -
What is regular expression for cheking a set operation
I want a regular expression for checking the syntax of the set operation given as input
ex:
the input should be
[1,2,3]+[2,3] or [1,2]-[2] or [1,2,3]*[1,2]
is should check the square bracket and the operator between two set operandsI think you should code that from scratch. When you validate input, you usually want to tell the user what they did wrong, but a regex will only tell you whether it matches or not.
-
Hi All,
What should be the regular expression for string MT940_UB_*.txt and MT940_MB_*.txt to be used as filename inSFTP sender channel in PI 7.31 ??
If any one has any idea on this please let me know.
Thanks
NehaHi All,
None of the file names suggested is working.
I have tried using - MT940_MB_*\.txt , MT940_MB_*.*txt , MT940*.txt
None of them is able to pick this filename - MT940_MB_20142204060823_1.txt
Currently I am using generic regular expression which picks all .txt files. - ([^\s]+(\.(txt))$)
Let me know ur suggestion on this.
Thanks
Neha Verma -
Regular Expression for a Person's Name
Hi,
I am using the org.apache.regexp package and trying to find the regular expression for a person's name. It allows only the alphabetic string.
I tried [a-zA-Z]+. But this also accepts the thing like "BUSH88", which is not what I want...
Can anybody help me figure this out?
Thanks in advance,
TongHi,
I am using the org.apache.regexp package and trying to
find the regular expression for a person's name. It
allows only the alphabetic string.
I tried [a-zA-Z]+. But this also accepts the thing
like "BUSH88", which is not what I want...
Can anybody help me figure this out?
Thanks in advance,
Tongtry this:
^[a-zA-Z]+$
the ^ represents the start of the String and the $ represents the end.
So the expression is saying: "between the beginning and the end of the String there will only be alphbetical characters" -
How to write the regular expression for Square brackets?
Hi,
I want regular expression for the [] Square brackets.
I have tried to insert in the below code but the expression not validate the [] square brackets.
If anyone knows please help me how to write the regular expression for [] Square brackets.
private static final Pattern DESC_PATTERN = Pattern.compile("({1}[a-zA-Z])" +"([a-zA-Z0-9\\s.,_():}{/&#-]+)$");Thanks
RaghavSince square brackets are meta characters in regex they need to be escaped when they need to be used as regular characters so prefix them with \\ (the escape character).
Maybe you are looking for
-
E-mail attachment problem of ios 4.3
I have the exchange 2010 mail account and recived by iPhone, everything okay before I upgrade the IOS to 4.3... Now I upgrade the IOS to 4.3, my iPhone can't open the attachment by 3G, it can download, you can see it will finish download job but can'
-
Can't POST FormData from iBooks widget on iPad
I have a Dashcode widget that simply submits contact information from a three-field form to a PHP script on our server. It works properly when run/debugged from Dashcode. But when I drop it into iBooks Author and preview it on an iPad (either v2 or v
-
Firefox will no longer save a Bitmap. When I click "SAVE AS" , nothing happens.
When I right-click on an Internet image, and on the drop-down box, select SAVE AS. No box appears to show the current name of the image, or to allow me to enter a name of my choice. ... and image is not save anywhere. Up to now it has always worked p
-
Hi, any way to locate dead, orphaned, tracks in our music library in itunes? I don't want to erase them, just locate them. Cheers M
-
Does time capsule back-up files which I am currently working on in a program?
Hey everybody, If I am working on a file in a program, does time capsule back this file up? So if the program(third party) crashes after half an hour for example, can I still get the file back or would it be incomplete because it was open during back