1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
xxMikexx [17]
4 years ago
5

Write a regular expression that matches positive real numbers starting and ending with a digit and possibly including a decimal

point – for example, 0.0035, 1.94, or 47 . There should be no leading zero in the case of numbers >=1, and there should be a single leading zero for numbers less than one. Your regular expression should use concatenation, + for alternation ("or"), and * for closure ("zero or more.") You may also use "[0-9]" for a single digit.

Computers and Technology
1 answer:
Tom [10]4 years ago
4 0

Answer:

#section 1

import re

pattern = r'^(\d+)(\.?)(\d+)$'

pattern2 = r'(^0+)(\d+)$'

pattern3 = r'^(0)(\.)(\d+)$'

tex = '0.0035 is coming 1.94 hopefully 47 home 000657 with 00.543'

print(a)

tex = tex.split()

ls = []

#section 2

for i in tex:

   if re.match(pattern, i):

       if float(i) < 1 and re.match(pattern3, i):

           ls.append(i)

       elif float(i) >= 1 and not re.match(pattern2, i):

           ls.append(i)

       

print(ls)

Explanation:

The programming language used is python 3.

#section 1

This is a regular expression question and the first thing to do will be to import the regular expression module.

The regular expression patterns are then created

pattern = r'^(\d+)(\.?)(\d+)$'  This pattern traps all digits integers of decimals

pattern2 = r'(^0+)(\d+)$'  This pattern traps all digits without a decimal point that have a leading zero(0)

pattern3 = r'^(0)(\.)(\d+)$'  This pattern traps all digits that are less than one and have a decimal point and just one zero(0) in front of the decimal.

REGULAR EXPRESSION TIPS:

^ means starts with

$ means ends with

+ means one or more occurrences

? means zero or one occurrences

\d represents digits from 0-9 you can also use [0-9]

\ is used to escape special characters.

() represents a group

note: the dot(.) is a special character in other to include it we must escape it

with this tips you will be able to understand the patterns.

A string containing some digits is passed to the tex variable to test the code.

The string is split up into a list using the split() method and an empty list is created to hold the result.

#section 2

I make use of for loops and if statements to compare each item in the list to each of the patterns.

In simple words, for each element in the list, if that element matches pattern1 , it is  a number.

The code moves to the next if statement to check if it's less than 1 and checks the leading zeros to ensure it is only 1, if it agrees with all the conditions, it is added to the new list.

if it does not, it goes to the elif statement to check if it is greater or equal to 1 and has no leading zeros. if that happens to be true. it is also added to the new list.

Finally, the result is printed to the screen.

check the picture to see code at work.

You might be interested in
Can you list one property of each of the following?<br> Excel<br> Word<br> Powerpoint
Rudiy27
Exel is used for graphing, Word for any type of essay or paper, and Powerpoint for presentations.
Hope this helps!
4 0
3 years ago
Read 2 more answers
Which of the following is considered a benefit when using slab allocator?
tatyana61 [14]

Answer:

Option(d) is the correct answer to the given question

Explanation:

The main advantage of slab allocator is that There is no memory is lost because of the fragmentation this is due to some driver data framework has a cache memory that is connected to it.

  • In the slab allocator the resources are repeatedly assigned or free to handle them. It is a type of memory management scheme that is used for distributing resources to the memory.
  • All the other options are not considered as advantages of Slab allocator that's why they are an incorrect options.
7 0
3 years ago
Question 5. 5. A computer network is BEST described as two or more computers that are: linked together. In the same room. a comp
melomori [17]
A computer network is best described as two or more computers that are linked together.
8 0
3 years ago
A driver that approaches a controlled intersection with a signal that is not working must:
nalin [4]
You must stop at the intersection and proceed when you are aware that other turning or approaching vehicles,bicycles or pedestrians have stopped.
5 0
3 years ago
What is the name of the general ledger that tracks all bitcoin transactions?.
motikmotik

Answer:

The Name of the General Ledger that Tracks all Bitcoin Transactions is Called the Blockchain.

A Blockchain is a Growing List of Records, known as Blocks, that are linked together using Cryptography. Each Block in a Blockchain contains a cryptographic Hash of the Previous Block, a timestamp, and also the transaction Data.

3 0
3 years ago
Read 2 more answers
Other questions:
  • The following if statement uses an overloaded &gt; operator to determine whether the price of a Car object is more than $5000. C
    15·1 answer
  • If r is an instance of the above Person class and oddNum has been declared as a variable of type boolean, which of the following
    8·1 answer
  • Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 then the outp
    12·1 answer
  • Write a while loop that prints
    13·1 answer
  • Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2, the
    7·1 answer
  • What do GUI use except​
    10·1 answer
  • Brian needs to see the space available to insert content on a slide in his presentation which feature of the presentation should
    10·1 answer
  • What is lossless compression
    6·2 answers
  • What are some of the key system-oriented trends that have fostered is-supported decision making to a new level?
    5·1 answer
  • Which of the following is NOT a best practice to protect data on your mobile computing device?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!