47069

regular expression for given pattern?

I need two regular expressions, on that matches anything that ends with 10PL that is (a single whitespace) (any integer) (the string PL in caps) (line terminates). I tried the following but none of them works. For eg.

var str="Visit W3Schools 45PL"; // should match this var str1="Visit W3Schools 45PL Other"; // should NOT match this var str2 = "Any Random value 133PL" // should match this var str3 = "Any Random value 133Pl" // should NOT match this

The other should match 21.323X230 (a single whitespace) (any floating value) (the word X in caps) (any other floating point value) (line terminates). For eg.

var test="Visit W3Schools 4X5"; // should match this var test1="Visit W3Schools 4X5PL Other"; // should NOT match this var test2 = "Any Random value 13.270X46.96" // should match this var test3 = "Any Random value 13.21X12.36 " // should NOT match this, as last word is white space

I tried the following pattern (for the first one (the PL one)).

var patt1=/\s+\d+/PL/g; var patt2 = /[ ]+[0-9]+/PL/g; document.write(patt1.test(str)); document.write(patt2.test(str)); document.write(patt1.test(str1)); document.write(patt2.test(str1)); document.write(patt1.test(str2)); document.write(patt2.test(str2)); document.write(patt1.test(str3)); document.write(patt2.test(str3));

result was all null (document.write didn't wrote anything). So, can anyone help me figure out regex for these two patterns?

Answer1:

You have a syntax error in your pattern - the / in the middle of the regex is

    <li>not needed (there is no literal slash you want to match, is there?), and </li> <li>confusing to the regex engine because it interprets it as the ending regex delimiter. It then tries to interpret PL/g as regex modifiers (which of course doesn't work).</li> </ul>

    Also, you're not telling the regex that it should match that pattern only at the end of a line. So try this:

    var patt1 = / \d+PL$/gm;

    $ matches (together with the m modifier) the end of a line. Without that modifier, it would only match the end of the string.

    For the second one:

    var patt2 = / \d+(?:\.\d+)?X\d+(?:\.\d+)?$/gm;

    Also, please don't visit W3Schools. It's one of the most error-ridden places on the internet. If you don't believe me, check out http://w3fools.com.

    Answer2:

    This should do it.

    var str = "Visit W3Fools 45PL"; // should match this var str1 = "Visit W3Fools 45PL Other"; // should NOT match this var str2 = "Any Random value 133PL" // should match this var str3 = "Any Random value 133Pl" // should NOT match this var test = "Visit W3Fools 4X5"; // should match this var test1 = "Visit W3Fools 4X5PL Other"; // should NOT match this var test2 = "Any Random value 13.270X46.96"; // should match this var test3 = "Any Random value 13.21X12.36 "; // should NOT match this, as last word is white space var patt1 = /\s\d+PL$/; document.write( patt1 + ' =~ "' + str + '" => ' + patt1.test(str) + '<br>'); document.write( patt1 + ' =~ "' + str1 + '" => ' + patt1.test(str1) + '<br>'); document.write( patt1 + ' =~ "' + str2 + '" => ' + patt1.test(str2) + '<br>'); document.write( patt1 + ' =~ "' + str3 + '" => ' + patt1.test(str3) + '<br>'); var patt2 = /\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/; document.write( patt2 + ' =~ "' + test + '" => ' + patt2.test(test) + '<br>'); document.write( patt2 + ' =~ "' + test1 + '" => ' + patt2.test(test1) + '<br>'); document.write( patt2 + ' =~ "' + test2 + '" => ' + patt2.test(test2) + '<br>'); document.write( patt2 + ' =~ "' + test3 + '" => ' + patt2.test(test3) + '<br>');

    The page writes out as:

    /\s\d+PL$/ =~ "Visit W3Fools 45PL" => true /\s\d+PL$/ =~ "Visit W3Fools 45PL Other" => false /\s\d+PL$/ =~ "Any Random value 133PL" => true /\s\d+PL$/ =~ "Any Random value 133Pl" => false /\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/ =~ "Visit W3Fools 4X5" => true /\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/ =~ "Visit W3Fools 4X5PL Other" => false /\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/ =~ "Any Random value 13.270X46.96" => true /\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/ =~ "Any Random value 13.21X12.36 " => false

Recommend

  • Notepad++ wildcard
  • MySQL Markup all records on Select with same values (first one included)
  • Play notification sound using URL
  • Importing data from JSON file to MySQL database using java [closed]
  • MATLAB map initialisation from a structure array
  • how to reduce width of column and position a JTable in java at desired coordinates
  • Trying to send a POST request to local server with C
  • How to fill in observations using other observations R or Stata
  • Select List of Column Names / Aliases from Custom Sub-Query
  • Declaring a scalar inside an if statement?
  • How can I extract lines of text from a file?
  • installing modules when using perlbrew
  • How to get Getopt::Long + pod2usage working?
  • Save SQL to YAML as is
  • add an element to a node, if it doesnot exist
  • Error(5,3): PLS-00103: Encountered the symbol “BEGIN” when expecting one of the following: language
  • How can I open a Windows CMD window for Perl and run a command?
  • Is there anything wrong with using requires after output starts printing?
  • Making turtles wait x number of ticks
  • Flex/AS3 very strange simple Number operation issue
  • Create variables for indexing in matlab
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • jQuery: How to AJAXify WordPress Search?
  • Multicolored edittext hint
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • Z3: Convert between FP and BitVector?
  • Declaring variable dynamically in VB.net
  • Scrapy recursive link crawler
  • Display issues when we change from one jquery mobile page to another in firefox
  • Android screen density dpi vs ppi
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Deselecting radio buttons while keeping the View Model in synch
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Join two tables and save into third-sql
  • Perl system calls when running as another user using sudo
  • How to handle AllServersUnavailable Exception
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • need help with bizarre java.net.HttpURLConnection behavior