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
Similar Messages
-
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 -
"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 -
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
-
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> -
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/") ) { -
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.. -
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).
-
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? -
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.
-
Re:java regular expression for website
Hi All,
I am using jdeveloper 11.1.2.3.0
My requirement is that I have a website attribute I need the regular expression for the website attribute
to display the format www.google.com www.oracle.com.
Thanks,
SandeepHi Sandeep,
you can use the below code for website validation.
<af:inputText label="" id="time" simple="true" value="" contentStyle="width:100px;" maximumLength="100">
<af:validateRegExp pattern="^www[.][a-z]{1,15}[.](com|org)$"
messageDetailNoMatch="Website must be like www.google.com"
hint="Website Format: www.google.com"/>
</af:inputText>
as per your requirement you can change the pattern.
Thanks
Prabhat -
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" -
Need a regular expression for the text field
Hi ,
I need a regular expression for a text filed.
if the value is alphanumeric then min 3 char shud be there
and if the value is numeric then no limit of chars in that field.[0-9].
Any help is appriciated...
thanks
bharathi.Try the following in the change event:
r=/^[a-z]{1,3}$|^\d+$/i;
if (!r.test(xfa.event.newText))
xfa.event.change="";
Kyle
Maybe you are looking for
-
Hello all, I created a scenario in the back-end to allow the user to change the activity type in a confirmation of a Service Order. In MAM, I can create a new operation and select the activity type from a list, but I cannot change the activity type i
-
Help with building a JTree using tree node and node renderers
Hi, I am having a few problems with JTree's. basically I want to build JTree from a web spider. The webspide searches a website checking links and stores the current url that is being processed as a string in the variable msg. I wan to use this varia
-
OVI truncated many of my Outlook 2007 Contacts not...
I normally use PC Suite, but had to use OVI find out which Maps have been loaded on my cell. It then inadvertently synced OVI with my E72. Now ALL notes inside Contacts have been truncated to appr 128 characters. Everything just lost on both Outlook
-
Acrobat 8 install forced change to different Windows Default Printer
Immediately after installing Adobe 8 Pro my Windows default printer changed and I can't get it to stay set to the default that I want. Everytime I restart my computer the Windows default printer is set to the one it changed to after installing Acroba
-
<blockquote>Locking duplicate thread.<br> Please continue here: [/questions/835635]</blockquote> When I tried to update to Firefox4 it told me 4 wont work with OS10.2.8. Can I update Firefox 2.0.0.12 to an earlier version of Firefox than4? How? Thank